M.O.S. Studios Posted August 5, 2008 Share Posted August 5, 2008 hey guys im attempting my own cart. here is my prob, i have a script that reads $_GET dadt and places it into a array variable, how ever it only seem to work once, im havingtrouble getting the two to combine, here is my script here is the url it see has blahblah/cart.php?action=add&cat=hotrods&item_number=B001 <?php session_start(); $cart = $_SESSION['cart']; if($_GET['action']==add) { if($cart) { $cart .=",". array(cat=> $_GET['cat'], item_number=> $_GET['item_number'], qty=>1); } else { $cart=array(cat=> $_GET['cat'], item_number=> $_GET['item_number'], qty=>1); } $_SESSION['cart'] = $cart; } $cart=array($_SESSION['cart']); echo "catagory item qty <br/>"; echo $cart['cat']." ".$cart['item_number']." ".$cart['qty']; //echo "<br/>"." ".$cart['1']['cat']." ".$cart['1']['item_number']." ".$cart['1']['qty']; print_r ($cart); this first time i add that prod it give me this Array ( [0] => Array ( [cat] => hotrods [item_number] => B001 [qty] => 1 ) ) what i want, is if i click the prod again it will give me this Array ( [0] => Array ( [cat] => hotrods [item_number] => B001 [qty] => 1, [1] => Array ( [cat] => hotrods [item_number] => B001 [qty] => 1) instead it gives me this: Array ( [0] => Array,Array ) can any one see my mistake? ?> thanks, Jarred f. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 5, 2008 Share Posted August 5, 2008 Try: <?php session_start(); $cart = $_SESSION['cart']; if($_GET['action']==add) { $cart[]=array(cat=> $_GET['cat'], item_number=> $_GET['item_number'], qty=>1); $_SESSION['cart'] = $cart; } It'll be a multi-dimensional array of products. Quote Link to comment Share on other sites More sharing options...
M.O.S. Studios Posted August 5, 2008 Author Share Posted August 5, 2008 WOW! thanks for that amazingly fast reply!!! i tried your code and it gave me this error: Fatal error: [] operator not supported for strings in /home/montrea1/public_html/lm/cart/cart.php on line 7 here is the new code <?php session_start(); $cart = $_SESSION['cart']; if($_GET['action']==add) { $cart[]=array(cat=> $_GET['cat'], item_number=> $_GET['item_number'], qty=>1); $_SESSION['cart'] = $cart; } print_r ($cart); ?> any ideas? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 5, 2008 Share Posted August 5, 2008 Clear your session first. It's because of the old session code. Just clear it and start a new one. =P Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 5, 2008 Share Posted August 5, 2008 Also, change it to this: <?php session_start(); $cart = $_SESSION['cart']; if($_GET['action']==add) { $cart[]=array('cat'=> $_GET['cat'], 'item_number' => $_GET['item_number'], 'qty' =>1); $_SESSION['cart'] = $cart; } print_r ($cart); ?> There was just some less-than-optimal code because you forgot the ' ' around the array keys. I added them. Won't affect the error though, so you'll still need a new session. Quote Link to comment Share on other sites More sharing options...
M.O.S. Studios Posted August 5, 2008 Author Share Posted August 5, 2008 lol i was just about to post that i figured it out, lol THANKS SO MUCH, it took me all day to get it, and you not even 5 mins (thank includes that time it took you to write the post) Im so happy thanks again best regards jarred Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 5, 2008 Share Posted August 5, 2008 No problem, glad to help. Please mark this topic as solved. Also, just for reference (and to maybe reduce the need for another post), when you want to iterate over the cart, here's how you can do it: foreach ($_SESSION['cart'] as $row) { echo $row['item_number']; //etc, etc } That's how the array's formatted. 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.