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 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();
}
}
}