Morty222 Posted October 7, 2008 Share Posted October 7, 2008 A user will select a tournament to register for, then add a team, then I store the following into a session: $teamID; $tournyID; $gatefee; $cost; When they click add to cart I update the session using this: $_SESSION['cart'][]['team'] = $teamID; $_SESSION['cart'][]['tournament'] = $tournyID; $_SESSION['cart'][]['gatefee'] = $gatefee; $_SESSION['cart'][]['cost'] = $cost; My array comes out like this: Array ( [cart] => Array ( [] => Array ( [team] => 2137 [tournament] => 24490 [gatefee] => [cost] => 575 ) ) ) As you can see there is no KEY so when someone adds another tournament to their cart it overwrites this one. How can I get the KEY to number 0, 1, 2, etc... with each addition to the cart? Thanks in advance!!!! Link to comment https://forums.phpfreaks.com/topic/127444-php-array-session-help/ Share on other sites More sharing options...
azunoman Posted October 7, 2008 Share Posted October 7, 2008 $_SESSION['cart'][]['team'] = $teamID; [] is empty obviously...I just wrote pretty much the same code but I put a [$cartindex] that starts at 0 and increments for each new cart. If you only want one cart then put 0 or 1 in a variable and code: $_SESSION['cart'][$cartindex]['team'] = $teamID; Array ( [cart] => Array ( [0] => Array( [team] => 2137 [tournament] => 24490 [gatefee] => [cost] => 575 hope that helps a little. I have a lot more code to support the cartindex and you should test the refresh for double post to your array. when my post data is created I set the time created and when I check the post data I compare incoming time with my current cart and if they are equal then I don't create a new cart just the current cart index.... Link to comment https://forums.phpfreaks.com/topic/127444-php-array-session-help/#findComment-659315 Share on other sites More sharing options...
Morty222 Posted October 7, 2008 Author Share Posted October 7, 2008 Yes, that helps, but by just adding [$cartindex] it doesnt assign it a KEY, it leaves it blank still. Dont I have to tell it what $cartindex equals and if so, how do I set this? Link to comment https://forums.phpfreaks.com/topic/127444-php-array-session-help/#findComment-659325 Share on other sites More sharing options...
PFMaBiSmAd Posted October 7, 2008 Share Posted October 7, 2008 The empty [] needs to be on the end for php to automatically assign the next numeric key in a usable way. $_SESSION['cart']['team'][] = $teamID; Link to comment https://forums.phpfreaks.com/topic/127444-php-array-session-help/#findComment-659326 Share on other sites More sharing options...
azunoman Posted October 7, 2008 Share Posted October 7, 2008 yes...you must init for example $cartindex to something.... Link to comment https://forums.phpfreaks.com/topic/127444-php-array-session-help/#findComment-659328 Share on other sites More sharing options...
DarkWater Posted October 7, 2008 Share Posted October 7, 2008 Or, if you don't want to have a $cartindex...: $_SESSION['cart'][] = array('team' => $teamID, 'tournament' => $tourneyID); //etc That's the optimal way to achieve the effect you wanted. Link to comment https://forums.phpfreaks.com/topic/127444-php-array-session-help/#findComment-659329 Share on other sites More sharing options...
PFMaBiSmAd Posted October 7, 2008 Share Posted October 7, 2008 This is better than what I suggested. Link to comment https://forums.phpfreaks.com/topic/127444-php-array-session-help/#findComment-659331 Share on other sites More sharing options...
azunoman Posted October 7, 2008 Share Posted October 7, 2008 darkwater.... in your example, would by chance PHP increment [] starting at 0 and going forth from there? I spent a lot of time setting my array starting at 0 and adding one etc myself and probably if I understood array processing better could have saved myself lots of work. hope that is still on topic.. $_SESSION['cart'][] = array('team' => $teamID, 'tournament' => $tourneyID); //etc Link to comment https://forums.phpfreaks.com/topic/127444-php-array-session-help/#findComment-659332 Share on other sites More sharing options...
Morty222 Posted October 7, 2008 Author Share Posted October 7, 2008 This worked great, thank you very much. Link to comment https://forums.phpfreaks.com/topic/127444-php-array-session-help/#findComment-659335 Share on other sites More sharing options...
DarkWater Posted October 7, 2008 Share Posted October 7, 2008 darkwater.... in your example, would by chance PHP increment [] starting at 0 and going forth from there? I spent a lot of time setting my array starting at 0 and adding one etc myself and probably if I understood array processing better could have saved myself lots of work. hope that is still on topic.. $_SESSION['cart'][] = array('team' => $teamID, 'tournament' => $tourneyID); //etc Indeed, it would increment it. This code: $array['cart'][] = array('team' => 15, 'tournament' => 5); $array['cart'][] = array('team' => 12, 'tournament' => 7); $array['cart'][] = array('team' => 76, 'tournament' => 2); print_r($array); Gives: Array ( [cart] => Array ( [0] => Array ( [team] => 15 [tournament] => 5 ) [1] => Array ( [team] => 12 [tournament] => 7 ) [2] => Array ( [team] => 76 [tournament] => 2 ) ) ) Link to comment https://forums.phpfreaks.com/topic/127444-php-array-session-help/#findComment-659336 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.