【问题标题】:Updating cookie values更新 cookie 值
【发布时间】:2018-02-22 14:08:11
【问题描述】:

我有一个使用 cookie 的购物车,您可以通过转到产品详细信息页面并单击添加购物车将产品添加到其中。脚本需要为购物车中的数量添加+1,每次点击添加即可。我不知道为什么,但数量每次都是 1。

基本上,问题是:为什么它不更新 cookie 中的数量?

产品.php:

if(isset($_POST['add'])){
    if(!empty($_POST['m'])){

        if (isset($_COOKIE['cart'])){
            $cart = json_decode($_COOKIE['cart'], TRUE, 512, JSON_OBJECT_AS_ARRAY); // if cookie is set, get the contents of it
        } else{
            $cart = [];// else create an empty cart
        }    

        // append new product and add to cart
        $cart[$product['id']] = [];
        $cart[$product['id']]['m'] = empty($_POST['m']) ? 1 : $_POST['m'];
        if(!empty($cart[$product['id']]['quantity'])){
            $cart[$product['id']]['quantity'] += 1;
        } else {
            $cart[$product['id']]['quantity'] = 1;
        }

        setcookie('cart', json_encode($cart), time()+3600, '/');
    } else {
        $error = "U moet minimaal 1m invullen";
    }

}

同样在购物车本身,我需要能够修改数量,这个值是允许被覆盖的。

shoppingcart.php:

if(isset($_COOKIE['cart'])){
    $cart = json_decode($_COOKIE['cart'], TRUE, 512, JSON_OBJECT_AS_ARRAY);
} else {
    $cart = [];
}
// dd($cart);

if(isset($_POST['remove'])){
    unset($cart[$_POST['item']]);
    setcookie('cart', json_encode($cart), time()+3600, '/');
}

$list = $model->selectMultipleById($cart, 'carpet');

【问题讨论】:

  • 问题是什么?
  • 为什么数量会被覆盖而不是更新
  • 因为每次都从一个新数组开始? $cart[$product['id']] = []; 。所以$cart[$product['id']]['quantity'] 永远不存在。
  • 附带说明:为什么每次单击按钮时都向服务器发送信息?你知道 javascript 也可以添加和修改 cookie 吗?
  • 我不太会使用 javascript。你的意思是我首先需要检查 $cart[$product['id']] 是否已经存在,然后在 if 语句中更新数量?

标签: php arrays cookies


【解决方案1】:

试试这个:

// append new product and add to cart

//first test if you have the id in your cookie. if so: update qty +1
if(!empty($cart[$product['id']])){
    $cart[$product['id']]['quantity'] += 1;
} 
//else create a new item in the cookie
else {
    $cart[$product['id']] = [];
    $cart[$product['id']]['quantity'] = 1;
}
//now $cart[$product['id']]['m'] always exists, so you can update m
$cart[$product['id']]['m'] = empty($_POST['m']) ? 1 : $_POST['m'];

【讨论】:

  • 谢谢,我也尝试了一些方法,它也有效,但似乎需要完成的代码太多
  • 而且,恕我直言,您应该研究一下 javascript cookie。每次单击按钮时,它都会为您节省往返服务器的时间。
【解决方案2】:

这是我尝试过的,但我似乎在重复自己的代码。

if(isset($_COOKIE['cart'][$product['id']])){
                $cart[$product['id']]['m'] = empty($_POST['m']) ? 1 : $_POST['m'];
                if(!empty($cart[$product['id']]['quantity'])){
                    $cart[$product['id']]['quantity'] += 1;
                } else {
                    $cart[$product['id']]['quantity'] = 1;
                }
            } else {
                $cart[$product['id']] = [];
                $cart[$product['id']]['m'] = empty($_POST['m']) ? 1 : $_POST['m'];
                $cart[$product['id']]['quantity'] = 1;
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多