shamwowy Posted August 8, 2011 Share Posted August 8, 2011 Hi all, I'm building a simple shopping cart and my function is not returning the correct item count. It will correctly tell me if there are no items in the cart, however, if there is 1 or more items, the count always returns as 1, no matter how many items are actually in there. Here's the add item to cart code, which works fine: $product_id = $_GET['itemid']; $action = $_GET['action']; switch($action) { case "add": $_SESSION['cart'][$product_id]++; break; Then I'm using the following function in my header include file (which is included on every page) function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '0 Items in Cart'; } else { // Parse the cart session variable $items = explode(',','$cart[product_id]'); $s = (count($items) > 1) ? 's':''; return '<a href="cart.php">View Cart</a> <font color=white size=2>['.count($items).' item'.$s.']</font>'; } } This function always returns 1 item even if it's 4 or 5 items in the cart. My session[cart] contains all the items, as I can see when I go to display them all. I'm thinking I'm either not using explode() correctly here (it's treating everything inside the cart var as a single string even though there are commas in there) or I'm not supposed to be using explode at all based on the variable type. If anyone can help with this I'd really appreciate it. Many thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/244265-explode-problemwrong-function-to-use/ Share on other sites More sharing options...
Pikachu2000 Posted August 8, 2011 Share Posted August 8, 2011 A print_r() of $items should show that since you have $cart[product_id] in single quotes in the explode function, it is being read as a string literal. Should be: $items = explode( ',', $cart['product_id'] ); Quote Link to comment https://forums.phpfreaks.com/topic/244265-explode-problemwrong-function-to-use/#findComment-1254549 Share on other sites More sharing options...
shamwowy Posted August 8, 2011 Author Share Posted August 8, 2011 Thanks a bunch! This doesn't work (still returns just 1 item count), but it validates that explode is the right function. Also thanks for pointing me at print_r which I didn't know about. (New to PHP coming from CF) Quote Link to comment https://forums.phpfreaks.com/topic/244265-explode-problemwrong-function-to-use/#findComment-1254608 Share on other sites More sharing options...
shamwowy Posted August 8, 2011 Author Share Posted August 8, 2011 I should note that print_r($items) returns Array ( [0] => ) even if I have 5 things in the cart. This seems odd to me considering that when I use the cart to hit my database it's getting each ID from there, so it's in there somewhere. I will keep trying. Quote Link to comment https://forums.phpfreaks.com/topic/244265-explode-problemwrong-function-to-use/#findComment-1254611 Share on other sites More sharing options...
DavidAM Posted August 8, 2011 Share Posted August 8, 2011 You need to print_r($cart) and look at what is in the cart. You used $product_id to add items to your cart. So they are NOT going to be listed as 'product_id' (unless of course, you have products whose product ID is "product_id") Quote Link to comment https://forums.phpfreaks.com/topic/244265-explode-problemwrong-function-to-use/#findComment-1254620 Share on other sites More sharing options...
shamwowy Posted August 9, 2011 Author Share Posted August 9, 2011 Right you are kind sir. With print_r($cart) I get the following: Array ( [6] => 1 [12] => 1 ) That being productID 6 x1 and productID 12 x1. Trying to get my function to tell me that there are 2 items total. It looks like the comma delimeter is not being inserted into this, or does it just not display in the output of print_r ? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/244265-explode-problemwrong-function-to-use/#findComment-1254634 Share on other sites More sharing options...
DavidAM Posted August 9, 2011 Share Posted August 9, 2011 I didn't see any commas in the code you posted. However, count($cart); // Will tell you how many items (product id's) are in the cart array_sum($cart); // Will tell you the total quantity of all items in the cart Quote Link to comment https://forums.phpfreaks.com/topic/244265-explode-problemwrong-function-to-use/#findComment-1254664 Share on other sites More sharing options...
shamwowy Posted August 9, 2011 Author Share Posted August 9, 2011 Perfect, thanks!! (Also easier) Quote Link to comment https://forums.phpfreaks.com/topic/244265-explode-problemwrong-function-to-use/#findComment-1254677 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.