session_start();
$id=$_POST['id'];
$productByCode= Yii::app()->db->createCommand("SELECT * FROM products where id='".$id."'")->queryRow();
$itemArray = array($productByCode["id"]=>array('ItemName'=>$productByCode["ItemName"], 'id'=>$productByCode["id"], 'price'=>$productByCode["price"], 'quantity'=>$quantity));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["code"],$_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["code"] == $k)
$_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
}
}
else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
}
else {
$_SESSION["cart_item"] = $itemArray;
}
if(isset($_SESSION["cart_item"])){
$item_total = 0;
?>
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
?>
<tr>
<td><strong><?php echo $item["ItemName"]; ?></strong></td>
<td><?php echo $item["quantity"]; ?></td>
<td align=right><?php echo "$".$item["price"]; ?></td>
</tr>
<?php
$item_total += ($item["price"]*$item["quantity"]);
}
?>
<tr>
<td colspan="5" align=right><strong>Total:</strong> <?php echo "$".$item_total; ?></td>
</tr>
</tbody>
</table>
<?php
}
i have above example code, it shows result when we click on add to cart product, n stores in session
Name Quantity Price
Banana 1 $50.00
Banana 1 $50.00
Banana 1 $50.00
Banana 1 $50.00
Banana 1 $50.00
Banana 1 $50.00
Total: $300
But i want to display like this, when we click on add to cart button on product, only quantity increase not repeat the products.
Name Quantity Price
Banana 6 $50.00
Total: $300
Aucun commentaire:
Enregistrer un commentaire