Jump to content
Killersites Forums

Problem With Locating Existing Items In Cart, Keeps Adding Another Row


Recommended Posts

I have some weird problem here,

 

The code is:

if($cart_count == 0)
{

    $_SESSION['cart'][] = $tmp_product;

} else if($cart_count > 0) {

    // if the array has anything in it:

    for($i=0; $i<$cart_count; $i++)
    { 
        // loop through the array rows:

        // if the id of the product already is in there then add to it:
        if($_SESSION['cart'][$i]['id'] === $tmp_product['id'])
        {
            // adds to the already there qty: preincrement actually I think
            $_SESSION['cart'][$i]['qty'] += $tmp_product['qty'];                

        } else {

            // otherwise add to it regardless of the conditional kind of...
            $_SESSION['cart'][] = $tmp_product;

        }
    }
}

The problem I am having is when I add the same product again it updates the qty that's fine works perfectly but if I was to go and add another product say product 2 as opposed to product 1 (that was added just prior) it keeps adding rows to the array, what's wrong with my logic to suggest it does this?

 

It must be something pretty obvious I am missing I just can't see it.

 

I appreciate anyone's help,

Jeremy.

Link to post
Share on other sites

Are you following any specific tutorials? Or is this your own code?

 

The first thing I notice is this section:

if($_SESSION['cart'][$i]['id'] === $tmp_product['id'])
{
// adds to the already there qty: preincrement actually I think
$_SESSION['cart'][$i]['qty'] += $tmp_product['qty']; 
}

I'm not sure how the use of $i there makes sense... you'll just end up with random numbers (from the for loop) with no specific meaning in your array. Are you sure that isn't supposed to be $id instead? Since that would make a lot more sense.

Link to post
Share on other sites
			// we need all the details of the product so we can add it to the cart (the needed data that means)
			$sql = sprintf("SELECT id, name, price FROM products WHERE id = '%d'", $prodid);
			
			// query the products table:
			$result = mysql_query($sql);
			
			// if no result send the user back the the home page:
			if(!$result)
			{
			
				header("Location: index.php");
			
			// else construct the ability to add the product to the cart, if the result is true and counts for 1 row:
			} else if ($result && mysql_num_rows($result) == 1) {
				
				// get the one row:
				$row = mysql_fetch_row($result);
				
				$tmp_product = array('id'=>$row[0],
						'name'=>$row[1],
						'price'=>$row[2],
						'qty'=>$_POST['qty']);
			}
			
			$cart_count = count($_SESSION['cart']);
			
			if($cart_count == 0)
			{
				
				$_SESSION['cart'][] = $tmp_product;
				
			} else if($cart_count > 0) {
				
				// if the array has anything in it:
				
				for($i=0; $i<$cart_count; $i++)
				{ // loop through the array rows:
					
					// if the id of the product already is in there then add to it:
					if($tmp_product['id'] == $_SESSION['cart'][$i]['id'])
					{
						// adds to the already there qty: preincrement actually I think
						$_SESSION['cart'][$i]['qty'] += $tmp_product['qty'];
					
					} else if ($tmp_product['id'] != $_SESSION['cart'][$i]['id']) {
						
						// otherwise add to it regardless of the conditional kind of...
						$_SESSION['cart'][] = $tmp_product;
						
					}
				}
			} // end of adding product

That's the whole process for the add to cart part of the code I wrote, it's really kind of half going off what I can remember (or lack of lol) what's in 2 different books as a kind of inspiration.

 

I appreciate your next reply,

Jeremy.

Link to post
Share on other sites

Yea I did that and sadly they're both comparing so would return to be true, so there's nothing wrong within itself just it's not behaving the way I'd want it to.

 

I actually played around with another example another developer was using and came up with this that works:

		case 'add':
			
			// we need all the details of the product so we can add it to the cart (the needed data that means)
			$sql = sprintf("SELECT id, name, price FROM products WHERE id = '%d'", $prodid);
			
			// query the products table:
			$result = mysql_query($sql);
			
			// if no result send the user back the the home page:
			if(!$result)
			{
			
				header("Location: index.php");
			
			// else construct the ability to add the product to the cart, if the result is true and counts for 1 row:
			} else if ($result && mysql_num_rows($result) > 0) {
				
				// get the one row:
				$row = mysql_fetch_row($result);
				
				$tmp_product = array('id'=>$row[0],
						'name'=>$row[1],
						'price'=>$row[2],
						'qty'=>$_POST['qty']);
			}
			
			$cart_count = count($_SESSION['cart']);
			
			if($cart_count === 0)
			{
				
				$_SESSION['cart'][] = $tmp_product;
				// this is fine!
				
			} else  {
				
				$already_in_cart = false;
				
				for($i=0; $i<$cart_count; $i++)
				{ // loop through the array rows:
					
					// if the id of the product already is in there then add to it:
					if($_SESSION['cart'][$i]['id'] === $tmp_product['id'])
					{
						
						$_SESSION['cart'][$i]['qty'] = $_SESSION['cart'][$i]['qty'] + $tmp_product['qty'];
						
						$already_in_cart = true;
						break;
					  
					}
				}
				
				if($already_in_cart === false)
				{
					$_SESSION['cart'][] = $tmp_product;
				}
				
			} // end of adding product
		break;

Thank you so much for your help Ben.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...