如果会话购物车项目超过1则不起作用..但它只有一个工作。发生了什么
我将所有代码放入会话中。检查这种情况会发生什么......
这是我的链接?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 ) )
答案 0 :(得分:1)
你在这条线上犯了一个错误。 if($ _ GET [“id”] == [$ k])
if (parseObject != nil)
{
if let votes = parseObject!.objectForKey("votes") as? Int {
cell.votesLabel?.text = "\(votes) votes"
}
else
{
cell.votesLabel?.text = "0 votes"
}
}
return cell}
答案 1 :(得分:0)
而不是
if($_GET["id"] == $k)
这样做
if($_GET["id"] == $_SESSION["cart_item"][$k]['id'])
答案 2 :(得分:0)
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"]);
}
}
它有效!!