【问题标题】:How to change a specific value of a array thats inside a session如何更改会话内的数组的特定值
【发布时间】:2020-01-01 16:43:24
【问题描述】:

(对不起我的英语不好) 单击按钮后,我正在尝试更改数组中的数量值

我尝试在网络上搜索帮助,但我发现的所有主题都不使用会话以及我在该代码中找到的那一堆东西(我在互联网上找到了该代码)

if (isset($_POST["add_to_cart"])) {
    if (isset($_SESSION["shopping_cart"])) {
        $item_array_id = array_column($_SESSION["shopping_cart"], "item_id");
        if (!in_array($_GET["id"], $item_array_id)) {
            $count      = count($_SESSION["shopping_cart"]);
            $item_array = [
                'item_id'       => $_GET["id"],
                'item_name'     => $_POST["hidden_name"],
                'item_price'    => $_POST["hidden_price"],
                'item_quantity' => $_POST["quantity"],
            ];
            $_SESSION["shopping_cart"][$count] = $item_array;
        } else {
            echo '<script>alert("Item Already Added")</script>';
            echo '<script>window.location="foodlist.php"</script>';
        }

当我点击提交时,这个“add_to_cart”被设置并且所有信息都被发送到另一个页面,但是当我再次点击它以添加另外 1 个项目(我之前点击的同一个项目)时,代码不会产生总和.我在其他方面尝试了很多东西,但即使是我的老师也无法帮助我:/

【问题讨论】:

    标签: javascript php arrays session


    【解决方案1】:

    这里有购物车类示例:

    <?php
    class Cart
    {
        private $cart = array();
    
        function __construct()
        {
            $this->cart = $_SESSION['cart'];
        }
    
        public function addProduct($id, $price)
        {
            $this->cart['products'][$id]['price'] = $price;
            $this->cart['products'][$id]['quantity'] = $this->cart['products'][$id]['quantity'] + 1; 
        }
    
        public function removeProduct($id)
        {
            $this->cart['products'][$id]['quantity'] = $this->cart['products'][$id]['quantity'] - 1; 
            if($this->cart['products'][$id]['quantity'] == 0){
                unset($this->cart['products'][$id]);
            }
        }
    
        public function delProduct($id)
        {
            unset($this->cart['products'][$id]);    
        }
    
        public function showCart()
        {
            echo "<pre>";
            print_r($this->cart);
            echo "</pre>";
        }
    
        function sumCart(){
            $sum = 0;
            foreach ($this->cart['products'] as $k => $item) {
                $sum = $sum + ((float) $item['price'] * (int) $item['quantity']);
            }
            return $sum;
        }
    
        // Save cart to session
        public function saveCart()
        {
            $_SESSION['cart'] = $this->cart;
        }
    }
    
    $cart = new Cart();
    // Add products
    $cart->addProduct(1,123.55);
    $cart->addProduct(9,93.00);
    $cart->addProduct(33,1.22);
    // Save to session
    $cart->saveCart();
    // Show cart
    $cart->showCart();
    // Show sum
    echo "Sum " . $cart->sumCart();
    ?>
    

    像这样试试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      • 2014-06-04
      • 1970-01-01
      • 2014-02-22
      相关资源
      最近更新 更多