如果会话购物车项目多于1个,则不起作用..但只有一个工作。这会发生什么
我把所有代码都放在了会话中。检查这种情况下会发生什么...
这是我的PHP代码此链接 ?action=remove&id=<value>
function checkCartForItem($addItem, $cartItems) {
if (is_array($cartItems)){
foreach($cartItems as $key => $item) {
if($item['id'] === $addItem)
return $key;
}
}
return false;
}
if (!empty($_GET['qty'])) {
$qty = $_GET['qty'];
}
//Store it in a Array
$ITEM = array(
//Item name
'id' => $_POST['id']
);
$addItem = $_GET['id'];
//check
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
case "add":
if(!empty($_GET["qty"])) {
$productByCode = $db_handle->runQuery("SELECT * FROM product WHERE id='" . $_GET["id"] . "'");
$itemArray = array($productByCode[0]["id"]=>array('name'=>$productByCode[0]["product_name"], 'id'=>$productByCode[0]["id"], 'quantity'=>$qty, 'price'=>$productByCode[0]["new_price"]));
$itemExists = checkCartForItem($addItem, $_SESSION['cart_item']);
if ($itemExists !== false){
$_SESSION['cart_item'][$itemExists]['quantity'] = $qty ;
} else {
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["id"],$_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["id"] == $k)
$_SESSION["cart_item"][$k]["quantity"] = $qty;
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
}
break;
case "remove":
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($_GET["id"] == $k)
unset($_SESSION["cart_item"][$k]);
if(empty($_SESSION["cart_item"]))
unset($_SESSION["cart_item"]);
}
}
break;
case "empty":
unset($_SESSION["cart_item"]);
break;
}
}
数组(print_r($_SESSION["cart_item"]);
)
Array ( [0] => Array ( [name] => Sadfsafsadf [id] => 11 [quantity] => 1 [price] => safsafsa ) [1] => Array ( [name] => TP-LINK 4 Port Wireless Dual Band N600 [id] => 13 [quantity] => 1 [price] => 15980 ) )
您在这条线上犯了一个错误。if($ _ GET [“ id”] == [$ k])
case "remove":
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($_GET["id"] == $_SESSION["cart_item"][$k]['id'])
unset($_SESSION["cart_item"][$k]);
if(empty($_SESSION["cart_item"]))
unset($_SESSION["cart_item"]);
}
}
break;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句