Skip to content

Create an “add to shortlist” feature for your website – part 1

July 19, 2009

Today I have been working on adding an ‘add to shortlist’ feature for Perfect Venue. The idea is that users can add several venues to a shortlist, and then email them all at once instead of having to use the contact form for each venue. It is kind of reminiscent of an ‘add to cart’ function on an E-commerce site, except that we are emailing everyone on our shortlist, instead of buying everything in our shopping cart (which I have been doing far too much lately).

The ‘add to shortlist’ function is accomplished via the use of PHP session variables, which store data that can be made available across multiple pages on your website.

The session variable for my shortlist is defined like this: $_SESSION['shortlist']

So here’s the first part:

$added = strpos($_SESSION['shortlist'], $_GET['id'].',');
$added = false;

This checks to see if $_SESSION[‘shortlist’] is already set, then checks if the id of the current venue is already in the shortlist. The ID of each venue (or of any arbitrary item you might use) is defined in the URL, therefore accessible via $_GET[‘id’]. If the current ID isn’t in our session variable then $added (the result of the strpos function) is false. If $_SESSION[‘shortlist’] doesn’t exist yet, then we manually set $added to false. You will see why the comma is appended to $_GET[id’] further below.

Now for part two.

If the user clicks on the ‘add to shortlist’ link, the current page is reloaded with a variable called ‘session’ in the URL, accessible via $_GET.

If the current ID doesn’t already exist in the session variable then we want to add it:

if($added === false)
//user has clicked 'add to shortlist'
/*...and there are already some venue ids in the shortlist, so just append the current one*/
$shortlist = $_SESSION['shortlist'];
$shortlist .= $_GET['id'].',';
//...or there are no venues on the shortlist currently, so set it to the current id
$shortlist = $_GET['id'].',';
/*either way, display 'added to shortlist' and then store $shortlist as a session variable*/
echo "Added to shortlist. <a href='shortlist.php'>View your shortlist</a>.";

If the user hasn’t clicked on ‘add to shortlist’ and the current ID isn’t already in the session variable, then we display the ‘add to’ link.

echo "<a href='currentpage.php?id=".$_GET&#91;'id'&#93;."&shortlist=add'>Add to shortlist</a>. <a href='shortlist.php'>View your shortlist</a>.";

So if the user clicks on ‘add to shortlist’ then above we have two possibilities after the first if statement (i.e. if the current ID isn’t already in the shortlist):

  1. $_SESSION[‘shortlist’] exists, and so we need to append the current ID to whatever is already stored.
  2. $_SESSION[‘shortlist’] doesn’t exist, so we just set it to the current ID.

Each ID in the session variable is separated by a comma, which makes it easy to split the string up into an array when you actually want to display each item in your list. It is crucial there is a comma after each ID to allow us to check if the current ID already exists in the session variable. This is used in the first step above. For example, if we are viewing an item with an ID of 2, and there is an item with ID of 200 already in $_SESSION[‘shortlist’], we have to make sure we search the string using the ID number and the comma otherwise we’ll get a true result on the strpos search if 200 exists, even if 2 doesn’t.

There are two further possibilities, and we need to display the correct message/links for these.

1. No IDs have been added to the shortlist

echo "<a href='currentpage.php?id=".$_GET&#91;'id'&#93;."&shortlist=add'>Add to shortlist</a>";

2. The current ID is already in the shortlist

elseif($added !== false)
echo "Added to shortlist. <a href='shortlist.php'>View your shortlist</a>";

What you have now is a session variable with a string of IDs, separated by commas, that you can then use however you like. As a general idea, you can split your comma separated string up and pop the IDs into an array (using something like explode). You might then want to use the IDs to query a mysql database and return some data on your shortlist.php page.

** UPDATE! See “Create an “add to shortlist” feature for your website – part 2” for how I did this.

Disclaimer: this is my first attempting at code blogging. I’ve probably f**ked up somewhere. If that is the case, don’t hesitate to point it out.

Oh, and remember to add <?php session_start(); ?> to every page on your site where you want to use session variables.

5 Comments leave one →
  1. Sasha permalink
    August 9, 2009 10:40 pm

    This is great, I have been looking for a script with the exact functions of yours, except I cant get it to work, it will not add to the shortlists page. Could you help please and let me know all the code? Thanks Sasha

  2. August 10, 2009 6:46 pm

    Hi Sasha, thanks for your comment 🙂 I’ll post part two for the shortlist feature for you shortly. Remember you’ve got to add the php session start code to the top of every page where you will be calling your session variables, and that this should come before any HTML, before the head tags.

  3. Sasha permalink
    August 10, 2009 6:55 pm

    Look forward to part two. Thank you


  1. Create an “add to shortlist” feature for your website – part 2 « Geek's the new black

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: