简体   繁体   English

如何更改会话内部的数组的特定值

[英]How to change a specific value of a array thats inside a session

(Sorry for my bad English) I'm trying to change a quantity value inside an array after clicking a button (对不起我的英语不好)我试图在单击按钮后更改数组内的数量值

I tried searching for help on the web, but all topics I found don't use session and that bunch of things that I found in that code (I found that code on the internet) 我尝试在网络上寻求帮助,但是发现的所有主题都没有使用会话,而是在该代码中找到了很多东西(我在互联网上找到了该代码)

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>';
        }

When I click submit, this "add_to_cart" is set and all information is sent to another page, but when I click it again to add 1 more item (the same item I clicked before) the code doesn't make a sum. 当我单击提交时,将设置此“ add_to_cart”并将所有信息发送到另一页,但是当我再次单击它以添加另外1个项目(我之前单击的相同项目)时,代码不会得出总和。 I tried a lot of things in this else, but even my teacher couldn't help me :/ 我在其他方面尝试了很多事情,但是即使我的老师也帮不了我:/

Here you have cart class sample: 这里有购物车类示例:

<?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();
?>

Try like this. 尝试这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM