我正在使用购物车项目,用户可以将多个商品添加到购物车中并从购物车中删除商品我在添加一个商品后使用会话我逐个增加ID,每个添加新商品到购物车中代码是工作是唯一的问题在删除当我试图从项目托盘中删除项目时它只删除一项是我的代码
<?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;
}
}
?>
这是我的删除按钮代码
<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>
答案 0 :(得分:0)
尝试此操作。在向购物车添加商品时,您需要使用$item_id_session
作为密钥。
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;