Omzy Posted October 7, 2009 Share Posted October 7, 2009 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 Quote Link to comment Share on other sites More sharing options...
Omzy Posted October 7, 2009 Author Share Posted October 7, 2009 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.. Quote Link to comment Share on other sites More sharing options...
btherl Posted October 8, 2009 Share Posted October 8, 2009 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']. Quote Link to comment Share on other sites More sharing options...
Omzy Posted October 8, 2009 Author Share Posted October 8, 2009 The thing with that is, $options is a long string, I don't fancy using that as an array key. 'item_number' is just a 3 digit number so it's ideal to have as an array key.. Quote Link to comment Share on other sites More sharing options...
btherl Posted October 8, 2009 Share Posted October 8, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.