Jump to content

Save to favorites


Marty Walker
 Share

Recommended Posts

Hi all,

I might be working on a property website where the client would like their site visitors to be able to save one or more properties to a favourites list. They won't be a logged in user of the site, just a casual visitor.

I'm wondering if any of you have done anything along these lines and where one would go to start going about it?

Cheers

Martyi

Link to comment
Share on other sites

Hey Marty,

If they're not logged in, then I think the only reliable option will be cookies. You just need to store the ID of the PW page that stores the property's details. You could store these in one cookie as a comma separated list.

I have done this with an an image library application so that guest users can create a lightbox of images.

You could get a little fancier and allow them to create more than one group of properties too.

Let me know if that helps, or if you'd like I can grab some non-PW bits of code as examples of how to store and retrieve the IDs.

I am sure you know this, but remember that you can use both PHP and JS to manipulate cookies, so you can save changes to their list of properties without needing a page refresh.

Just make sure the expiry of the cookie is set to a year or something like that.

Hope that provides a starting point.

  • Like 1
Link to comment
Share on other sites

Hey Marty - sorry for taking so long to get back to you - busy day :)

So you could do this with Ajax calls to a PHP script like the following. It would be called whenever someone clicks on a property. This would be a toggle approach, which may or may not be appropriate depending on how the site is set up. In this case, the cookie would store an array of the property IDs.

$pid = $page->id;

if (!in_array($pid, $favs)) { //add to the array if not already in it
    $favs[] = $pid;
} 
else {
    $key = array_search($pid, $favs);
    unset ($favs[$key]); //remove from array if already in it
}

$data = base64_encode(serialize($favs));
setcookie('property_favs', $data, time() + 86400 * 100, '/');

Then to read the cookie back in, something like this:

$favs = unserialize(base64_decode($_COOKIE['property_favs']));

That will give you an array of page ids that you can use to populate the user's favourites.

If you want to go with a combination of javascript and PHP, you can do that also. Firstly, I recommend taking a look at this jquery helper:

https://github.com/carhartl/jquery-cookie

You can add the property/page id to the cookie using that and then retrieve on page load using PHP.

Instead of storing an encoded array, you can also store a separated list of IDs. This example is using raw javascript:

if(readCookie('property_favs')){
    document.cookie = 'property_favs=' + readCookie('property_favs') + escape('|' + pid) + '; path=/';
}
else{
    document.cookie = 'property_favs=' + escape('|' + pid) + '; path=/';
    }

So if the cookie exists, then read it and add on a new pid after a pipe character and escape it so it can be stored in the cookie. If the cookie doesn't exist, create it with the current pid. The jquery cookie plugin will make this cleaner.

Remember to use your developer console as it will show you the current cookie contents. In Chrome it is one of the options under Resources. In Firefox I seem to remember installing a plugin for Firebug to analyze cookies.

It's really pretty easy once you play around with it.

Let us know if you have any specific questions.

  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...