i am working with aa cart item where user can add multiple item into the cart and delete the item from cart i am using session after adding one item i am increment id one by one on each add new item into cart the code is working is fine only problem in deleting when i am trying to delete the item from item tray its delete only one item here is my code
<?php
session_start();
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
case "add":
if(!isset($_SESSION['cart'])) {
$item_id_session = 1;
$_SESSION['cart'] = array();
}
else
{
$count = count($_SESSION['cart']);
$item_id_session = $count++;
}
$item = array();
$item['item_id_session']= $item_id_session ;
$item['title']=$_REQUEST['title'];
$item['length_m'] =$_REQUEST['length_m'];
$item['length_md']=$_REQUEST['length_md'];
$item['length_cm'] =$_REQUEST['length_cm'];
$item['length_cmd']=$_REQUEST['length_cmd'];
$item['width_m']=$_REQUEST['width_m'];
$item['width_md']=$_REQUEST['width_md'];
$item['width_cm']=$_REQUEST['width_cm'];
$item['width_cmd']=$_REQUEST['width_cmd'];
$item['height_m']=$_REQUEST['height_m'];
$item['height_md']=$_REQUEST['height_md'];
$item['height_cm']=$_REQUEST['height_cm'];
$item['height_cmd']=$_REQUEST['height_cmd'];
$item['weight']=$_REQUEST['weight'];
$item['weightd']=$_REQUEST['weightd'];
$item['quantity']=$_REQUEST['quantity'];
$item['image']=$_FILES["image"]["name"];
$target_dir = "profile_images/";
$item['image']= "profile_images/" . $_FILES["image"]["name"];
$imageFileType = pathinfo($item['image'],PATHINFO_EXTENSION);
$check = getimagesize($_FILES["image"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
//$uploadOk = 1;
move_uploaded_file($_FILES["image"]["tmp_name"], $item['image']);
echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
}
$_SESSION['cart'][] = $item;
break;
case "remove":
if(!empty($_SESSION["cart"])) {
foreach($_SESSION["cart"] as $k => $v) {
if($_GET['item_id_session'] == $k)
unset($_SESSION["cart"][$k]);
unset($_SESSION['cart'][$item_id_session]);
if(empty($_SESSION["cart"]))
unset($_SESSION["cart"]);
}
}
break;
}
}
?>
here is my delete button code
<a href="step3.php?action=remove&id=<?php echo $item["item_id_session"];?>"class="product-title">
<span class="glyphicon glyphicon-trash pull-right"></span>
</a>
Try this.while adding items to the cart you need to use $item_id_session
as key.
case "add":
if (isset($_SESSION['cart']) && count($_SESSION['cart']) > 0) {
$count = count($_SESSION['cart']);
$item_id_session = $count+ 1;
} else {
$item_id_session = 1;
$_SESSION['cart'] = array();
}
$item = array();
$item['item_id_session']= $item_id_session ;
$item['title']=$_REQUEST['title'];
$item['length_m'] =$_REQUEST['length_m'];
$item['length_md']=$_REQUEST['length_md'];
$item['length_cm'] =$_REQUEST['length_cm'];
$item['length_cmd']=$_REQUEST['length_cmd'];
$item['width_m']=$_REQUEST['width_m'];
$item['width_md']=$_REQUEST['width_md'];
$item['width_cm']=$_REQUEST['width_cm'];
$item['width_cmd']=$_REQUEST['width_cmd'];
$item['height_m']=$_REQUEST['height_m'];
$item['height_md']=$_REQUEST['height_md'];
$item['height_cm']=$_REQUEST['height_cm'];
$item['height_cmd']=$_REQUEST['height_cmd'];
$item['weight']=$_REQUEST['weight'];
$item['weightd']=$_REQUEST['weightd'];
$item['quantity']=$_REQUEST['quantity'];
$item['image']=$_FILES["image"]["name"];
$target_dir = "profile_images/";
$item['image']= "profile_images/" . $_FILES["image"]["name"];
$imageFileType = pathinfo($item['image'],PATHINFO_EXTENSION);
$check = getimagesize($_FILES["image"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
//$uploadOk = 1;
move_uploaded_file($_FILES["image"]["tmp_name"], $item['image']);
echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
}
$_SESSION['cart'][$item_id_session] = $item;
break;
case "remove":
if(!empty($_SESSION["cart"])) {
foreach($_SESSION["cart"] as $k => $v) {
if($_GET['item_id_session'] == $k)
unset($_SESSION["cart"][$k]);
}
}
break;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.