[英]php shopping cart session id remove
如果會話購物車項目多於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;
代替
if($_GET["id"] == $k)
做這個
if($_GET["id"] == $_SESSION["cart_item"][$k]['id'])
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"]);
}
}
有用!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.