【问题标题】:php update quantity on cart pagephp更新购物车页面上的数量
【发布时间】:2015-06-21 17:39:33
【问题描述】:

我想更新购物车中商品的数量

这些是动作:

   if(!empty($_GET["action"])) {
    switch($_GET["action"]) {
        case "add":
            if(!empty($_POST["quantity"])) {
                $productByCode = $db_handle->runQuery("SELECT * FROM menu WHERE item_code='" . $_GET["code"] . "'");
                $itemArray = array($productByCode[0]["item_code"]=>array('name'=>$productByCode[0]["item_name"], 'code'=>$productByCode[0]["item_code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["item_price"]));

                if(!empty($_SESSION["cart_item"])) {
                    if(in_array($productByCode[0]["item_code"],$_SESSION["cart_item"])) {
                        foreach($_SESSION["cart_item"] as $k => $v) {
                                if($productByCode[0]["item_code"] == $k)
                                    $_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
                        }
                    } else {
                        $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
                    }
                } else {
                    $_SESSION["cart_item"] = $itemArray;
                }
            }
        break;

        case "remove":
            if(!empty($_SESSION["cart_item"])) {
                foreach($_SESSION["cart_item"] as $k => $v) {
                        if($_GET["code"] == $k)
                            unset($_SESSION["cart_item"][$k]);              
                        if(empty($_SESSION["cart_item"]))
                            unset($_SESSION["cart_item"]);
                }
            }
        break;
        case "empty":
            unset($_SESSION["cart_item"]);
        break;  
    }
    }

这是购物车代码:

<?php       
    foreach ($_SESSION["cart_item"] as $item){
        ?>
                <tr>
                <td><strong><?php echo $item["name"]; ?></strong></td>
                <td><?php echo $item["quantity"]; ?></td>
                <td><?php echo "&#8377;".$item["price"]; ?></td>
                <td><a id="remove" href="index.php?action=remove&code=<?php echo $item["code"]; ?>"><span class="fa fa-times fa-lg" style="color:red;"></span></a></td>
                </tr>
                <?php
        $item_total += ($item["price"]*$item["quantity"]);
        }
        ?>

我需要像点击更新按钮时这样的东西,这样它就可以获取值并更新特定产品数量的会话数组 请帮帮我...

【问题讨论】:

  • 那么问题出在哪里?你有错误吗?正在发生什么,期望是什么。
  • 在检查会话时使用array_key_exists 而不是in_array
  • @Jon Stirling 我曾尝试使用添加操作来更新数量,但效果不佳。
  • 你能帮忙写一个更新案例吗?
  • 进展不顺利?这意味着什么?它收拾东西离开了吗?你必须具体。

标签: php session cart


【解决方案1】:

这是一个非常基本的示例,但您应该为每个操作创建一个函数,以便更轻松地移植购物车。另外,我不确定你为什么在switch 中有一个$_GET,在case() 中有一个$_POST

function AddToCart($default = 1)
    {
        // Set a default quantity (1). Check that the value is set and is number
        // Not sure why you are using a get for the code but not for these
        // attributes. Should it not be $_GET['quantity']??
        $qty                    =   (isset($_POST['quantity']) && is_numeric($_POST['quantity']))? $_POST['quantity']:$default;
        // Check that there is a valid numeric itemcode
        $item                   =   (isset($_POST['item_code']) && is_numeric($_POST['item_code']))? $_POST['item_code']:false;
        // If false, stop
        if(!$item)
            return;
        // Do your product query
        $productByCode          =   $db_handle->runQuery("SELECT * FROM menu WHERE item_code='" . $_GET["code"] . "'");
        // Assign the item code
        $itmcd                  =   $_GET["code"];
        $itemArray['code']      =   $itmcd;
        $itemArray['name']      =   $productByCode[0]["item_name"];
        $itemArray['price']     =   $productByCode[0]["item_price"];
        $itemArray['quantity']  =   (isset($_SESSION["cart_item"][$itmcd]))? $_SESSION["cart_item"][$itmcd]['quantity']+$qty : $qty;

        $_SESSION["cart_item"][$itmcd]  =   $itemArray;
    }

使用方法:

if(isset($_GET["action"]) && $_GET["action"] == 'add'))
    AddToCart();

【讨论】:

    猜你喜欢
    • 2013-06-05
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    相关资源
    最近更新 更多