my goal is when user adds a product thats already in the cart, it will retain the product name and it will add 1 to the current quantity

my code is:

$item_found = false;

$index_of_array = 0;

if(isset($_POST['product_id']))
{
	$product_id = $_POST['product_id'];

	if(!isset($_SESSION['cart_array'] ) || empty($_SESSION['cart_array'] ))
	{
		$_SESSION['cart_array'] = array(1 => array("product_id" => $product_id, "quantity" => 1));
	}
	else
	{
		foreach($_SESSION['cart_array'] as $each_item)
		{
			$index_of_array++;
			
			while(list($key, $value) = each($each_item))
			{
				if($key == "product_id" && $value == $product_id)
				{
					array_splice($_SESSION['cart_array'], $index_of_array-1, 1, array(array("product_id" => $product_id, "quantity" => $each_item['quantity']+1)));
					$item_found = TRUE;
				}
			}
			if($item_found == FALSE)
			{
				array_push($_SESSION['cart_array'], array("product_id" => $product_id, "quantity" => 1));
			}
		}
	}
}

Dani AI

Generated

Brief context and quick fix summary tied to the thread: noted the problem was solved but didn’t post the solution; suggested posting it — that’s the right move because the symptom shown (array_splice “not working”) is commonly caused by a couple of subtle issues: using the old while/list/each pattern, and treating the cart as if it had 0-based numeric indexes while it doesn’t. array_splice will reindex numeric keys, so using it to update a single item can change keys unexpectedly. (php.net)

Avoid each() and while(list(...)=each(...)) — that construct is deprecated/removed in modern PHP and can hide pointer/index bugs; foreach (with an index) is simpler and future-proof. (php.net)

A small, robust pattern that solves the “increment quantity if product exists, otherwise append” logic without array_splice:

if (!isset($_SESSION['cart_array']) || !is_array($_SESSION['cart_array'])) {
    $_SESSION['cart_array'] = [];
}

$found = false;
foreach ($_SESSION['cart_array'] as $i => $row) {
    if (isset($row['product_id']) && $row['product_id'] == $product_id) {
        $_SESSION['cart_array'][$i]['quantity'] = (int)$row['quantity'] + 1;
        $found = true;
        break;
    }
}

if (! $found) {
    $_SESSION['cart_array'][] = ['product_id' => $product_id, 'quantity' => 1];
}

Extra tips: initialize the cart as a plain indexed array (use [] or $_SESSION['cart_array'][] to append), do not push inside the loop (push only after the whole search), and if using foreach by reference remember to unset($var) afterwards to avoid accidental references. For modifying elements in-place, foreach with the index (or by-reference) is the recommended approach. (php.net)

Recommended Answers

All 4 Replies

lets see if code will be more clear:

$item_found = false;
 
$index_of_array = 0;
 
if(isset($_POST['product_id']))
{
	$product_id = $_POST['product_id'];
 
	if(!isset($_SESSION['cart_array'] ) || empty($_SESSION['cart_array'] ))
	{
		$_SESSION['cart_array'] = array(1 => array("product_id" => $product_id, "quantity" => 1));
	}
	else
	{
		foreach($_SESSION['cart_array'] as $each_item)
		{
			$index_of_array++;
 
			while(list($key, $value) = each($each_item))
			{
				if($key == "product_id" && $value == $product_id)
				{
					array_splice($_SESSION['cart_array'], $index_of_array-1, 1, array(array("product_id" => $product_id, "quantity" => $each_item['quantity']+1)));
					$item_found = TRUE;
				}
			}
			if($item_found == FALSE)
			{
				array_push($_SESSION['cart_array'], array("product_id" => $product_id, "quantity" => 1));
			}
		}
	}
}
Member Avatar for Member #120589

so what's problem?

i figured it out. thanks! anyway I could delete this post?

Member Avatar for Member #120589

You could ask, but I doubt whether the mods/admins will do it. How about you post your solution and mark the thread solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.