soundcyst Posted December 16, 2007 Share Posted December 16, 2007 I'm building a simple session based shopping cart that interfaces with Google Checkout. The addtocart.php file is called as the action to a form on each product's page. I'm pretty sure the code has proper logical functionality (if there's no cart, create one, if there is a cart, see if the new item is already there. if it is, increment quantity, if not, add it to the list), and based on the output in the cart.php file, it looks like everything is working EXCEPT incrementing the quantity. It's not adding duplicate entries of the same item, but the quantity never goes above 1. here is the code: <?php session_start(); $newentry['item'] = $_POST['item']; $newentry['description'] = $_POST['description']; $newentry['qty'] = intval($_POST['qty']); $newentry['price'] = $_POST['price']; $newflag = 1; if ($_SESSION['cart']) { foreach ($_SESSION['cart'] as $entry) { if (!strcmp($entry['item'], $newentry['item'])) { $_SESSION['cart'][$entry]['qty']++; $newflag = 0; } } if ($newflag) { $_SESSION['cart'][] = $newentry; } } else { $_SESSION['cart'][] = $newentry; } header('Location: ./cart.php'); ?> and here is the block that displays the cart <table> <?php $cart = $_SESSION['cart']; foreach ($cart as $item) { $num = $item['qty']; $name = $item['item']; $price = $item['price']; $total = $num * $price; echo "<tr><td>$num</td><td>$name</td><td>$price</td><td>$total</td>\n"; } ?> </table> I have googled around, but not been able to find much. I've also tried assigning $_SESSION['cart'][$entry]['qty'] to a temporary variable, incrementing the temporary variable, and then reassigning it to $_SESSION['cart'][$entry]['qty'], which is not working either. Any insight would be greatly appreciated. Thanks! Quote Link to comment Share on other sites More sharing options...
acidglitter Posted December 16, 2007 Share Posted December 16, 2007 maybe change $_SESSION['cart'][$entry]['qty']++; to $_SESSION['cart'][$entry]['qty']=$_SESSION['cart'][$entry]['qty']+1? i don't know why but sometimes $variable++; doesn't work.. Quote Link to comment Share on other sites More sharing options...
corbin Posted December 16, 2007 Share Posted December 16, 2007 Print out SESSION and you'll see where everything is going wrong.... foreach ($_SESSION['cart'] as $entry) { if (!strcmp($entry['item'], $newentry['item'])) { $_SESSION['cart'][$entry]['qty']++; $newflag = 0; } This is taking $entry references the value of $_SESSION['cart'], so unless the value of $_SESSION['cart'][] = the key, this is invalid. Try: foreach ($_SESSION['cart'] as $key => $entry) { if (!strcmp($entry['item'], $newentry['item'])) { $_SESSION['cart'][$key]['qty']++; $newflag = 0; } Also, it could be more effecient if instead of making random keys for new items, you used the value from $_SESSION['cart'][<random key>] = values including item as $_SESSION['cart'][<item>] = values That would, you could just see if isset($_SESSION['cart'][$id]) when you're checking to see if it exists or not. Sorry for the vagueness in that along with the random vars and stuff, but hopefully it made sense ;p. Quote Link to comment Share on other sites More sharing options...
soundcyst Posted December 16, 2007 Author Share Posted December 16, 2007 Thanks for the replies! I am confused about a couple of things though This is taking $entry references the value of $_SESSION['cart'], so unless the value of $_SESSION['cart'][] = the key, this is invalid. $_SESSION['cart'] is first assigned with a $_SESSION['cart'][] = $somearray, so doesn't foreach $_SESSION['cart]' as $entry make $entry = [1:n] for n entries? isn't this effectively the same as (through a single iteration): $entry = $_SESSION['cart'][n]; //meaning $entry['item'] == $_SESSION['cart'][n]['item'] .... As I'm typing this out i'm starting to see why maybe this doesn't work... since $entry is an array, referencing $_SESSION['cart'][$entry][anything] isn't the same as $_SESSION['cart'][0][anything] when $entry is the 0th array, right? Also, it could be more effecient if instead of making random keys for new items, you used the value from $_SESSION['cart'][<random key>] = values including item as $_SESSION['cart'][<item>] = values That would, you could just see if isset($_SESSION['cart'][$id]) when you're checking to see if it exists or not. the first part, about random keys, confuses me. i'm not making random keys, i'm making sequential keys depending on when the user ads them. are you suggesting a product numbering scheme? second, don't you sill need a for loop to execute all of the issets()? is a call to isset() less than a strcmp()? Anyways, thanks, I will try the $key => $entry suggestion and see how things turn out. Quote Link to comment Share on other sites More sharing options...
soundcyst Posted December 16, 2007 Author Share Posted December 16, 2007 ok, that worked to solve my problem. i am still a little confused about the foreach ($array as $key => $entry) when $array is more than 1 dimensional. does this just make $array = $entry and $key = i, meaning that $array[$key] == $entry? basically it just extracts the element of the array too? thanks again for the help! 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.