Duplicate Session Array Values

Hello All

I am having an issue with finding duplicate array values that have been stored inside a php session i would like it when the person displays the items in the cart i would like it to display 1 x Product A and 5 X Product B

instead of
Product A
Product B
Product B
Product B
Product B
Product B

Here what i have so far


include("dbconnect.php");
	$cQuery="SELECT * FROM products";
	$result=mysqli_query($con,$cQuery);;
	if(!$result)
	{
		echo "Error:".mysqli_error($con);
	}
	else
	{
		$count=$result->num_rows;
		if($count>0)
		{
			while($data=$result->fetch_assoc())
			{
				$items[]=$data['Name'];
				$prices[]=$data['Cost'];
			}
		}
		else
		{
			echo "There is no products in the catalog,Please contact the webmaster of this store";
		}
	}
	//
	for ($i = 0; $i < count($_SESSION['cart']); $i++)
	{
		$Keys = $items[$_SESSION['cart'][$i]];
		echo count($Keys);
	}

What am i doing wrong can anyone help me fix this problem

i have attempted to do it another way but i am running into these errors

Warning: Invalid argument supplied for foreach() on line 20

Warning: array_push() [function.array-push]: First argument should be an array on line 34

The code i have is


$WasFound = false;
	$pid=$_GET['buy'];
	if (!isset($_SESSION['cart'])|| count($_SESSION['cart'])<1)
	{ // if no cart
		$_SESSION['cart'] = array(1=>array(""=>$pid,"Quantity"=>1)); // 					
	}
	else
	{
		foreach($_SESSION["cart"] as $each_item)
		{
			$i++;
			while(list($key,$value) = each($each_item))
			{
				if($key == "item_id" && $value == $pid)
				{
					array_splice($_SESSION['cart'],$i-1,1,array(array("item_id"=>$pid, "Quantity"=>$each_item['Quantity']+1)));
					$WasFound = true;
				}
			}
		}
		if($WasFound == false)
		{
			array_push($_SESSION['cart'],array("item_id"=>$pid,"Quantity"=>1));
		}
	}

Why would this cause a problem?


foreach($_SESSION["cart"] as $each_item)
{


What is wrong with that why is that returning back an error??

I don’t see a session start, try print_r($_SESSION); and see what you have to work with.

Also in foreach, echo out $each_item, that should dial you in.

i have this problem now too is that when i click on my buy now link for say my product A and in the database field in the database i have set it to 5 its always creating an extra one in the array

what i mean is if i have 5 of product A and i want to add another product A to my cart it shouldnt let me but it is still letting me i have started sessions that are started at the top and i have a function getting the field for Stock on Hand depending on the pid i have posted to it but not sure why its still adding another Product A to my cart it displays like this

Apple 5 $810
Apple 1 $162

that Apple product that has one under the one that has 5 shouldnt be there so why is it being added to my array again

Here is my PHP Code


	$WasFound = false;
	$i=0;
	$pid=$_GET['buy'];
	if (!isset($_SESSION['cart'])|| count($_SESSION['cart'])<1)
	{ // if no cart
		$_SESSION['cart'] = array(1=>array(""=>$pid,"Quantity"=>1)); // 					
	}
	else
	{
		
		foreach($_SESSION['cart'] as $each_item)
		{
			$i++;
			$stock = get_stock_check($pid);
			while(list($key,$value) = each($each_item))
			{
				if($key == "item_id" && $value == $pid)
				{
					$Qty = $each_item['Quantity'];
					//echo $Qty;
					//echo $stock;
					echo "i var is".$i."";
					if($Qty<$stock)
					{
						array_splice($_SESSION['cart'],$i-1,1,array(array("item_id"=>$pid, "Quantity"=>$each_item['Quantity']+1)));
						$WasFound = true;
					}
					else if($Qty>$stock)
					{
						echo "Product Out of Stock";
					}
					ReturnHome();					
				}
			}
		}
		if($WasFound == false)
		{
			echo "i var is".$i."";
			if($key == "item_id" && $value == $pid && $each_item['Quantity']==$stock)
			{
				//unset($_SESSION['cart'];
				echo 'product out of stock';	
			}
			else
			{
				array_push($_SESSION['cart'],array("item_id"=>$pid,"Quantity"=>1));
				ReturnHome();	
			}
		}
	}

For my stock check function its simply

include("dbconnect.php");
	$Query="SELECT * FROM products WHERE pid='$pid'";
	$rs= mysqli_query($con,$Query);
	if(!$rs)
	{
		echo "Error0".mysqli_error($con);
	}
	else
	{
		//
		$count=$rs->num_rows;
		if($count>0)
		{				
			
			while($data=$rs->fetch_assoc())
			{
				return "".$data['StockOnHand']."";
			}
		}
	}

So what am i doing wrong why is it not working why is it still adding same item in my cart when i have said there are only 5 in the database?