PHP会话数组 - 使用不同的值存储相同的项目

时间:2015-06-01 05:01:36

标签: php arrays multidimensional-array shopping-cart

我正在使用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);
}

期待一些有用的答案。

由于

***** UPDATE ******

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

//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);
}

3 个答案:

答案 0 :(得分:2)

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

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

而不是

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

检查数量的方法并不是一种好的做法,因为您可以在现有条目中单独编辑数量。

答案 1 :(得分:2)

您需要制定一个独特的购物车商品代码,以使其有效。要么您可以将产品代码与大小或数量相结合,要么使用类似GUID的东西来存储单独的购物车项目。

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

答案 2 :(得分:0)

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

$product = array();

希望这有帮助。

相关问题