Omzy Posted October 7, 2009 Share Posted October 7, 2009 What I'm looking to do is store multiple items in the SESSIONS array (the whole point of using sessions really). The items will be POSTed to cart.php, all items include fileds "item_name", "item_code", "item_price". How can I ensure the items are added uniquely to the SESSIONS array? Quote Link to comment https://forums.phpfreaks.com/topic/176847-solved-sessions-for-shopping-cart/ Share on other sites More sharing options...
RussellReal Posted October 7, 2009 Share Posted October 7, 2009 add them with item_code as the array key. $_SESSION[$item_code] = array($item_name,$item_price,$item_quantity); Quote Link to comment https://forums.phpfreaks.com/topic/176847-solved-sessions-for-shopping-cart/#findComment-932459 Share on other sites More sharing options...
Omzy Posted October 7, 2009 Author Share Posted October 7, 2009 Cheers. The next thing I need to do is enable quantities to be changed. I got this to work when there is only 1 item but it doesn't seem to work when there is more than 1 item: foreach($_SESSION as $value) { <input type="text" name="quantity_update" value="'.$value[2].'" size="2"/> <input type="hidden" name="item_number" value="'.$value[0].'"/> } $_SESSION[$_POST['item_number']] = array($_POST['item_number'], $_POST['item_name'], $_POST['quantity'], $_POST['amount']); if(isset($_POST['quantity_update'])) { $_SESSION[$_POST['item_number']][2] = $_POST['quantity_update']; } I think I need to put it into an array or something, but how? Quote Link to comment https://forums.phpfreaks.com/topic/176847-solved-sessions-for-shopping-cart/#findComment-932612 Share on other sites More sharing options...
RussellReal Posted October 7, 2009 Share Posted October 7, 2009 Ja, do sumfin like this: $_SESSION['items'][$item_code] = bla bla bla; den when you loop just do foreach ($_SESSION['items'] as $k => $v) { // $v will hold the array for whatever itemcodes are in your item list and $k will be your item_code } and you're gonna wanna echo something like dis: echo 'name="item_quantity['.$k.']"'; Quote Link to comment https://forums.phpfreaks.com/topic/176847-solved-sessions-for-shopping-cart/#findComment-932640 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.