【问题标题】:PHP Session Array - store same item with different valuesPHP Session Array - 存储具有不同值的相同项目
【发布时间】:2015-08-14 11:08:18
【问题描述】:

我正在开发一个 PHP 购物车系统,但遇到了问题。

问题:

当用户添加一个项目,然后再次添加相同的项目但具有不同的值(例如不同的大小或数量)时,购物车会使用用户选择的新值更新该条目。以前的详细信息被删除。

我一直在寻找的解决方案

如果用户添加了任何商品,然后想要添加相同的商品但有不同的要求,则应将其作为单独的条目添加到购物车会话中。(仅当特定变量发生更改时,例如:单个产品但有不同的尺寸)。

如何在我当前的代码中做到这一点?

购物车

//add item in shopping cart
if(isset($_POST["type"]) && $_POST["type"]=='add')
{
    $product_code   = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); //product code
    $product_size   = filter_var($_POST["product_size"], FILTER_SANITIZE_NUMBER_INT); //product size
    $product_qty    = filter_var($_POST["product_qty"], FILTER_SANITIZE_NUMBER_INT); //product quantity
    $return_url     = base64_decode($_POST["return_url"]); //return url


    //MySqli query - get details of item from db using product code
    $results = $connection->query("SELECT * FROM products WHERE prod_code='$product_code' LIMIT 1");
    $obj = $results->fetch_object();

    if ($results) { //we have the product info 

        //prepare array for the session variable
        $new_product = array(array('name'=>$obj->product_name, 'code'=>$product_code, 'size'=>$product_size, 'qty'=>$product_qty, 'price'=>$obj->price));

        if(isset($_SESSION["products"])) //if we have the session
        {
            $found = false; //set found item to false

            foreach ($_SESSION["products"] as $cart_itm) //loop through session array
            {
                if($cart_itm["code"] == $product_code){ //the item exist in array

                    $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'size'=>$product_size, 'qty'=>$product_qty, 'price'=>$cart_itm["price"]);
                    $found = true;
                }else{
                    //item doesn't exist in the list, just retrieve old info and prepare array for session var
                    $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'size'=>$cart_itm["size"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
                }
            }

            if($found == false) //we didn't find item in array
            {
                //add new user item in array
                $_SESSION["products"] = array_merge($product, $new_product);
            }else{
                //found user item in array list, and increased the quantity
                $_SESSION["products"] = $product;
            }

        }else{
            //create a new session var if does not exist
            $_SESSION["products"] = $new_product;
        }

    }

    //redirect back to original page
    header('Location:'.$return_url);
}

期待一些有用的答案。

谢谢

***** 更新 ******

从购物车中删除产品的代码:

//remove item from shopping cart
if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"]))
{
    $product_code   = $_GET["removep"]; //get the product code to remove
    $return_url     = base64_decode($_GET["return_url"]); //get return url


    foreach ($_SESSION["products"] as $cart_itm) //loop through session array var
    {
        //if($cart_itm["code"]!=$product_code){ //item does,t exist in the list
        if($cart_itm["code"] != $product_code && $cart_itm["size"] != $product_size){
            $product[] = array(
            'name'=>$cart_itm["name"],
            'code'=>$cart_itm["code"],
            'size'=>$cart_itm["size"],
            'qty'=>$cart_itm["qty"],
            'price'=>$cart_itm["price"]
            );
        }

        //create a new product list for cart
        $_SESSION["products"] = $product;
    }

    //redirect back to original page
    header('Location:'.$return_url);
}

【问题讨论】:

    标签: php arrays multidimensional-array shopping-cart


    【解决方案1】:

    您使用的if 条件是您犯的错误。试试这个

    if($cart_itm["code"] == $product_code && $cart_itm["size"] == $product_size)
    

    而不是

    if($cart_itm["code"] == $product_code)
    

    检查数量的方法不是很好的做法,因为您可以在已经存在的条目中单独编辑数量。

    【讨论】:

    • 现在项目单独添加,这是我正在寻找的答案。但是删除有问题。所有项目都被删除,因为$product_code 相同怎么办?我应用了同样的条件if($cart_itm["code"] !== $product_code && $cart_itm["size"] !== $product_size){
    • @yaqoob 您在评论中的情况与 sujivasagam 的情况既不相同也不相反。您所指的代码也未显示。请就该部分提出一个新问题
    • 在否定格式中使用与 'if($cart_itm["code"] != $product_code && $cart_itm["size"] != $product_size)' 相同的条件
    • @sujivasagam:我没有工作,我正在添加有问题的删除产品代码,请检查。
    • 需要更多解释。什么时候可以从购物车中删除商品?同时点击对应的删除按钮?
    【解决方案2】:

    您需要制定一个唯一的购物车商品代码才能使其正常工作。您可以将产品代码与尺寸或数量结合起来,或者使用 GUID 之类的东西来存储单独的购物车项目。

    组合键在更改大小或数量时出现问题。使用 GUID 需要做一些工作,但它是一个可行且可靠的解决方案。

    【讨论】:

    • 谢谢 holroy,我试试这个。
    • 用“项目删除”代码更新了我的问题
    【解决方案3】:

    尝试在开头将$product 声明为数组。

    $product = array();
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-08-12
      • 2018-10-12
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      相关资源
      最近更新 更多