简体   繁体   English

PHP购物车会话ID删除

[英]php shopping cart session id remove

if have session cart items more than 1 its not work.. but its have only one its work. 如果会话购物车项目多于1个,则不起作用..但只有一个工作。 what happen for this 这会发生什么

I put my all of codes in session. 我把所有代码都放在了会话中。 check what happen for this case... 检查这种情况下会发生什么...

here is my php code for this link ?action=remove&id=<value> 这是此链接的我的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;  
}
}

array ( print_r($_SESSION["cart_item"]); ) 数组( 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 ) )

You made a mistake on this line. 您在这条线上犯了一个错误。 if($_GET["id"] == [$k]) 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;

Instead of 代替

if($_GET["id"] == $k) 

do this 做这个

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"]);
        }
    }

it works!! 有用!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM