I have created a shopping cart in which i am able to add items in it but unable to remove the cart items.below is my code:
<?php session_start(); require_once("dbcontroller.php"); $db_handle = new DBController(); if(!empty($_GET["action"])) { switch($_GET["action"]) { case "add": if(!empty($_GET["qty"])) { $productByCode = $db_handle->runQuery("SELECT * FROM shop_product WHERE id='" . $_GET["product_code"] . "'"); $itemArray = array($productByCode[0][id]=>array('name'=>$productByCode[0]["in_product"], 'code'=>$productByCode[0][id], 'quantity'=>$_GET["qty"], 'price'=>$productByCode[0]["price"])); if(!empty($_SESSION["cart_item"])) { var_dump($productByCode[0][id]); if(in_array($productByCode[0][id],$_SESSION["cart_item"])) {//changed code to id //here id is not matching in cart_item thats why it is saving individual same products. foreach($_SESSION["cart_item"] as $k => $v) { if($productByCode[0][id] == $k) $_SESSION["cart_item"][$k]["quantity"] = $_GET["qty"]; } } else { $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray); } } else { $_SESSION["cart_item"] = $itemArray; } var_dump($itemArray); } break; case "remove": if(!empty($_SESSION["cart_item"])) { foreach($_SESSION["cart_item"] as $k => $v) { if($_GET["product_code"] == $k) unset($_SESSION["cart_item"][$k]); if(empty($_SESSION["cart_item"])) unset($_SESSION["cart_item"]); } } break; case "empty": unset($_SESSION["cart_item"]); break; } } ?>and I am using ajax also for adding,removing and emptying the cart. here is the code:
I think there is something in cart.php that is not matching. Kindly help me.