Combining values from PHP bucket
I have the following question.
I store the shopping cart in a PHP session and then load it to display the items in the cart.
The problem is that currently I see every item without consolidation, i.e. if an item is added 2 times to the cart it will be shown twice.
How can I consolidate the itemId results?
if(is_array($_SESSION['cart']))
{
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++)
{
$pid=$_SESSION['cart'][$i]['itemId'];
$q=$_SESSION['cart'][$i]['qty'];
if($q==0) continue;
$query2 = $con -> prepare("SELECT * FROM item_descr WHERE id_item = :idItem");
$query2-> bindValue (':idItem',$pid);
$query2->execute();
$row2 = $query2->fetch(PDO::FETCH_ASSOC);
?>
<div class="checkoutItems">
<span class='itemNameChck'><?php echo $row2['name']; ?> </a></span>
<span class="itemQtyChck"><?php echo $row2['price']; ?> </span>
<span class="itemQtyChck"><?php echo $q; ?> </span>
<span class="itemQtyChck"><?php $totalPerItem = $q * $row2['price']; echo $totalPerItem; ?> </span>
</div>
<?php
}
}
This is how I add items to my cart:
function addtocart($pid,$q)
{
if($pid<1 or $q<1) return;
if(is_array($_SESSION['cart']))
{
$max=count($_SESSION['cart']);
$_SESSION['cart'][$max]['itemId']=$pid;
$_SESSION['cart'][$max]['qty']=$q;
$max=count($_SESSION['cart']);
}
else
{
$_SESSION['cart']=array();
$_SESSION['cart'][0]['itemId']=$pid;
$_SESSION['cart'][0]['qty']=$q;
$max=count($_SESSION['cart']);
}
//}
}
thank
source to share
In my opinion you need to get rid of this id containing the cart index.
// I mean this
// ||
// ||
// \ /
// \/
$_SESSION['cart'][$max]['qty']=$q;
Just put itemId
there and before adding an element - check if this array element is set.
You wrote this code which adds a new element to the array every time and now you want to consolidate it. It doesn't make sense to me. Add a new element only when you need it. Otherwise, just update the quantity.
In my opinion, your cart should look like this:
$_SESSION['cart'][$itemid]['qty']=$q;
// example how to store other attributes
$_SESSION['cart'][$itemid]['name']="Rubber duck";
$_SESSION['cart'][$itemid]['color']="Yellow";
$_SESSION['cart'][$itemid]['size']="XXL";
$_SESSION['cart'][$itemid]['price']="1000";
Then when adding an item, you can check if the cart item contains this itemid of any items.
if isarray($_SESSION['cart'][$itemid]) // when exists - add to previous quantity
$_SESSION['cart'][$itemid]['qty']= $_SESSION['cart'][$itemid]['qty'] + $q;
else // else - set quantity
$_SESSION['cart'][$itemid]['qty'] = $q;
This is just a project / idea, but I hope this helps you.
source to share
Something like this should do the trick
function addtocart($pid,$q)
{
if($pid<1 or $q<1) return;
for($i=0;$i> count($_SESSION['cart']);$i++)
{
if($_SESSION['cart'][$i]["item_id"] == $pid)
{
$_SESSION['cart'][$i]['qty'] += $q
return;
}
}
if(is_array($_SESSION['cart']))
{
$max=count($_SESSION['cart']);
$_SESSION['cart'][$max]['itemId']=$pid;
$_SESSION['cart'][$max]['qty']=$q;
$max=count($_SESSION['cart']);
}
else
{
$_SESSION['cart']=array();
$_SESSION['cart'][0]['itemId']=$pid;
$_SESSION['cart'][0]['qty']=$q;
$max=count($_SESSION['cart']);
}
}
source to share