Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/244265-explode-problemwrong-function-to-use/
Share on other sites

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'] );

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.

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")

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.