如何使用cookies方法将多个项目添加到购物车

时间:2021-04-12 07:21:26

when i add second item to the cart , the first item will be replaced
this is my code(cart.php)

当我将第二个项目添加到购物车时,第一个项目将被替换这是我的代码(cart.php)

<?php 
    include"connect.php";
 ?>

<?php
   $total=0;
$variety = $quantity = $bran = "";

if(isset($_COOKIE['cart']))
{
$cookie = $_COOKIE['cart'];
$cookie = stripslashes($cookie);
$savedCardArray = json_decode($cookie, true);
foreach ($savedCardArray as $key => $value) {
    $variety=$savedCardArray[$key][0];
    $quantity=$savedCardArray[$key][1];
    $bran=$savedCardArray[$key][2];
    $total = $quantity*998;
    setcookie('total', $total);
}

}
?>

the cart_update page is given below

cart_update页面如下所示

<?php
class CartUpdate 
{

    /**
     * Initialize the Cart.
     * @return void
     */
    public function __construct() {

        if(isset($_COOKIE['cart'])) {

            $cookie = $_COOKIE['cart'];
            $cookie = stripslashes($cookie);
            $savedCardArray = json_decode($cookie, true);

         }
    }


    function add()
    {
        $variety = test_input($_POST["variety"]);  
        $rice_type = test_input($_POST["rice_type"]);
        $quantity = test_input($_POST["quantity"]);
        $bran = test_input($_POST["bran"]);
        $items[]=array($variety,$quantity,$bran,$rice_type);
        $json = json_encode($items);    
        setcookie('cart', $json);
    }

i need all selected items in cart with this cookies method. could anyone help me??

我需要使用此Cookie方法在购物车中选择所有商品。谁能帮助我?

1 个解决方案

#1


My PHP is a bit rusty these days but it seems every time you call add() method on Cart object you rewrite your cookie. In your Cart constructor you read cookie contents into local var $savedCardArray (typo?) but then you operate on local var $items in your add() method, add new element to it and save the cookie.

这些天我的PHP有点生疏,但似乎每次在Cart对象上调用add()方法都会重写你的cookie。在Cart构造函数中,您将cookie内容读入本地var $ savedCardArray(拼写错误?),但随后您在add()方法中操作本地var $ items,向其添加新元素并保存cookie。

I would suggest refactoring this code. First of all is there any special reason this class is called CartUpdate? Just name it Cart. In constructor read in cookie into instance variable so once you read the contents from cookie you can then access it in other methods of Cart instance. You can also provide get() method to return array of items in the cart and total() method that will calculate total price.

我建议重构这段代码。首先,这个类被称为CartUpdate有什么特殊原因吗?把它命名为Cart。在构造函数中将cookie读入实例变量,因此一旦从cookie中读取内容,您就可以在Cart实例的其他方法中访问它。您还可以提供get()方法以返回购物车中的项目数组以及将计算总价格的total()方法。

#1


My PHP is a bit rusty these days but it seems every time you call add() method on Cart object you rewrite your cookie. In your Cart constructor you read cookie contents into local var $savedCardArray (typo?) but then you operate on local var $items in your add() method, add new element to it and save the cookie.

这些天我的PHP有点生疏,但似乎每次在Cart对象上调用add()方法都会重写你的cookie。在Cart构造函数中,您将cookie内容读入本地var $ savedCardArray(拼写错误?),但随后您在add()方法中操作本地var $ items,向其添加新元素并保存cookie。

I would suggest refactoring this code. First of all is there any special reason this class is called CartUpdate? Just name it Cart. In constructor read in cookie into instance variable so once you read the contents from cookie you can then access it in other methods of Cart instance. You can also provide get() method to return array of items in the cart and total() method that will calculate total price.

我建议重构这段代码。首先,这个类被称为CartUpdate有什么特殊原因吗?把它命名为Cart。在构造函数中将cookie读入实例变量,因此一旦从cookie中读取内容,您就可以在Cart实例的其他方法中访问它。您还可以提供get()方法以返回购物车中的项目数组以及将计算总价格的total()方法。