在购物车会话中添加新内容

时间:2015-04-10 10:08:39

标签: php mysql session

我试图在我的会话中添加一系列产品,但所有代码都是替换我当前的产品,请帮我找到这个错误。当我点击按钮时,会话中的产品会更改为我刚刚输入的任何建议。这是我的代码如下。

<?php

require_once("../../init.php");

    function runQuery($query){
        $result = $db->query($query);
        while($row=mysqli_fetch_assoc($result)) {
            $resultset[] = $row;
        }       
        if(!empty($resultset))
            return $resultset;
    }

if(!empty($_GET["action"])) {
switch($_GET["action"]) {
    case "add":
        if(!empty($_POST["quantity"])) {
            $productByCode = mysqli_fetch_array($db->query("SELECT * FROM productpurchase WHERE productname='" . $_GET["productname"] . "'"));

            $itemArray = array($productByCode[0]["productname"]=>array('productname'=>$_POST["productname"],  'quantity'=>$_POST["quantity"], 'sell'=>$_POST["price"]));

            if(!empty($_SESSION["cart_item"])) {
                if(in_array($productByCode[0]["productname"],$_SESSION["cart_item"])) {
                    foreach($_SESSION["cart_item"] as $k => $v) {
                            if($productByCode[0]["productname"] == $k)
                                $_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
                    }
                } 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["productname"] == $k)
                        unset($_SESSION["cart_item"][$k]);              
                    if(empty($_SESSION["cart_item"]))
                        unset($_SESSION["cart_item"]);
            }
        }
    break;
    case "empty":
        unset($_SESSION["cart_item"]);
    break;  
}
}
?>

1 个答案:

答案 0 :(得分:1)

您正在以错误的方式添加产品。由于key中的产品名称为array,因此您应该像这样添加它。

$productName = $productByCode[0]["productname"];
$itemArray = array('productname' => $productName, 'quantity' => $_POST["quantity"], 'sell' => $_POST["price"]);
$_SESSION["cart_item"][$productName] = $itemArray;

此外,您的in_array if语句每次都会失败,因为购物车array值为arrays,您正在与string进行比较。使用array_keys获取array个产品名称。

将其更改为

if(in_array($productName, array_keys($_SESSION["cart_item"]))) {

您不需要foreach循环。

最终代码将是

<?php

// Add product name to variable, since we will use it often
// This would be key in cart array.
$productName = $productByCode[0]["productname"];

// Array with product data
$itemArray = array('productname' => $productName, 'quantity' => $_POST["quantity"], 'sell' => $_POST["price"]);

// Check if cart has some products already
if(!empty($_SESSION["cart_item"])) {

  // Is product already in cart? If so, edit only quantity
  if(in_array($productName, array_keys($_SESSION["cart_item"]))) {
    $_SESSION["cart_item"][$productName]["quantity"] = $_POST["quantity"];
  } else {
    // Product is not in cart, but we have other products in cart.
    // So just add to existing cart array.
    $_SESSION["cart_item"][$productName] = $itemArray;
  }
}
else {
    // No products, create cart session and add first product
    $_SESSION["cart_item"] = array();
    $_SESSION["cart_item"][$productName] = $itemArray;
}

?>