Jump to content

UPDATE $_SESSIONS


Omzy

Recommended Posts

Here is my SESSIONS array:

 

$_SESSION[$_POST['item_number']]=array($_POST['item_number'], $_POST['item_name'], $_POST['options'], $_POST['amount']);

 

Somewhere in this array I want to include a $quantity variable - the way the site is designed there is currently no quantity field on the product pages so I want to be able to set this on the checkout page as follows:

 

1) If the $_POST['item_number'] is POSTed for the first time, $quantity is set to 1.

 

2) If the same $_POST['item_number'] is POSTed again, the $quantity of that session item is increased

 

3) If the same $_POST['item_number'] is POSTed again but the $options variable is different then a new session item is added to $_SESSION

Link to comment
Share on other sites

OK I managed to figure out the first two but the last one is now proving to be very difficult - any help would be appreciated. It's difficult because $_POST['item_number'] has to be unique, so I probably need to add on a counter or something..

Link to comment
Share on other sites

It looks like it's not really item_number that defines an item, but "item_number + options".  So you might use a combination of both as a key, for example:

 

$key = $_POST['item_number'] . "|" . $_POST['options'];
$_SESSION['items'][$key] = array($_POST['item_number'], $_POST['item_name'], $_POST['options'], $_POST['amount']);

 

I've also added ['items'] into the session variable - the reason is that there may be a security issue by setting these items at the top level of $_SESSION.  If someone sends an unexpected value for item_number they might be able to overwrite other entries in $_SESSION.  If you know this won't be a problem then you can remove the ['items'].

Link to comment
Share on other sites

Well the problem with item_number is it doesn't uniquely identify an item.  So the question is do you store your items as "widget|red" and "widget|green" or "widget" - "red" and "widget" - "green".

 

The first option is using a combined key of item number and options.

 

The second option would fit a 2 level array like this:

 

$_SESSION[$_POST['item_number']][$_POST['options']] = ...;

 

From a technical perspective, there's nothing wrong with using a long string as an array key.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.