简体   繁体   English

如何增加同一产品的数量,以及如何减少数量?

[英]How to increase the quantity within the same product, and how to decrease the quantity?

The problem that I encountered was it it cannot increase while I clicking the adding button for the second time, and I don't have the idea of how to do the decrease button. 我遇到的问题是,当我第二次单击添加按钮时,它无法增加,而且我不知道如何执行减少按钮。

if (isset($_POST["add"])){
if (isset($_SESSION["cart"])){
    $item_array_id = array_column($_SESSION["cart"], "id");
    if(!in_array($_GET["id"], $item_array_id)){
        $count = count($_SESSION["cart"]);
        $item_array = array(
            'id' => $_GET["id"],
            'item_name' => $_POST["hidden_name"],
            'product_price' => $_POST["hidden_price"],
            'item_quantity' => $_POST["quantity"],
        );
        $_SESSION["cart"][$count] = $item_array;
        echo '<script>window.location="counter.php"</script>';
    }else{
        echo '<script>alert("Product is already Added to List")</script>';
        echo '<script>window.location="counter.php"</script>';
    }
}else{
    $item_array = array(
        'id' => $_GET["id"],
        'item_name' => $_POST["hidden_name"],
        'product_price' => $_POST["hidden_price"],
        'item_quantity' => $_POST["quantity"],
    );
    $_SESSION["cart"][0] = $item_array;
}

} }

<?php
        $conn = mysqli_connect("localhost", "root", "", "test");
        $query = "SELECT product_id, product_name, product_price, image FROM product ORDER BY product_id ASC ";
        $result = mysqli_query($conn,$query);
        if(mysqli_num_rows($result) > 0) {

            while ($row = mysqli_fetch_array($result)) {

                ?>
                <div class="col-md-3">

                    <form method="post" action="counter.php?action=add&id=<?php echo $row["product_id"]; ?>">

                        <div class="product">
                            <img src="img/<?php echo $row["image"]; ?>" style="width:100px; height:100px">
                            <h5 class="text-info"><?php echo $row["product_name"]; ?></h5>
                            <h5 class="text-danger"><?php echo "RM " . $row["product_price"]; ?></h5>
                            <input type="text" name="quantity" class="form-control" value="1">
                            <input type="hidden" name="hidden_name" value="<?php echo $row["product_name"]; ?>">
                            <input type="hidden" name="hidden_price" value="<?php echo $row["product_price"]; ?>">
                            <input type="submit" name="add" style="margin-top: 5px;" class="btn btn-success" value="+">
                            <input type="submit" name="minus" style="margin-top: 5px;" class="btn btn-success" value="-">
                        </div>
                    </form>
                </div>
                <?php
            }
        }
    ?>

<div style="clear: both"></div>
    <div class="table-responsive">
        <table class="table table-bordered">
        <tr>
            <th width="30%">Product Name</th>
            <th width="10%">Quantity</th>
            <th width="10%">Total Price</th>
            <th width="17%">Remove Item</th>
        </tr>

        <?php
            if(!empty($_SESSION["cart"])){
                $total = 0;
                foreach ($_SESSION["cart"] as $key => $value) {
                    ?>
                    <tr>
                        <td><?php echo $value["item_name"]; ?></td>
                        <td><?php echo $value["item_quantity"]; ?> <a href="counter.php?action1=subtract&id=<?php echo $value["product_id"]; ?>"><input type="submit" name="subtract" style="width:20px; height:25px" class="btn btn-success" 
                                value="-"></td>
                        <td>RM <?php echo number_format($value["item_quantity"] * $value["product_price"], 2); ?></td>
                        <td style="text-align:center;"><a href="counter.php?action=delete&id=<?php echo $value["product_id"]; ?>"><img src="img/icon-delete.png"/></a></td>

                    </tr>
                    <?php
                    $total = $total + ($value["item_quantity"] * $value["product_price"]);
                }
                    ?>
                    <tr>
                        <td colspan="2" align="right">Total</td>
                        <th align="right">RM <?php echo number_format($total, 2); ?></th>
                        <td></td>
                    </tr>
                    <?php
                }
            ?>
        </table>

    </div>

The output of adding only shows for the first clicked, but it will be displayed "Product is already Added to List" for the second clicked, cannot increase the product quantity. 添加的输出仅显示第一次单击的结果,但是第二次单击将显示“产品已添加到列表”,不能增加产品数量。 The actual result should be increasing quantity. 实际结果应该是数量增加。 Same question for the decrease product quantity 关于减少产品数量的相同问题

You have to change the logic of increment and decrement the quantity. 您必须更改增加和减少数量的逻辑。

If you press add button then increase the value of product by 1 and same vice versa. 如果按添加按钮,则将乘积的值增加1,反之亦然。 So first of all your need to get the current quantity of the added product and then do the operator add or delete. 因此,首先您需要获取当前添加产品的数量,然后由操作员添加或删除。

$_SESSION["cart"][$count]['item_quantity']=$_SESSION["cart"][$count]['item_quantity'] + $_POST["quantity"];  // For add

Basic idea given below. 下面给出了基本思路。

if (isset($_POST["add"])){
   if (isset($_SESSION["cart"])){
      $item_array_id = array_column($_SESSION["cart"], "id");
       if(!in_array($_GET["id"], $item_array_id)){
          $count = count($_SESSION["cart"]);
          $_SESSION["cart"][$count]['item_quantity']=$_SESSION["cart"][$count]['item_quantity']+$_POST["quantity"];
           $item_array = array(
                'id' => $_GET["id"],
                'item_name' => $_POST["hidden_name"],
                'product_price' => $_POST["hidden_price"],
                'item_quantity' => $_POST["quantity"],
           );
           $_SESSION["cart"][$count] = $item_array;
          echo '<script>window.location="counter.php"</script>';
      }else{
          echo '<script>alert("Product is already Added to List")</script>';
          echo '<script>window.location="counter.php"</script>';
      }
}else{
     $_SESSION["cart"][0]['item_quantity']=$_SESSION["cart"][0]['item_quantity']- $_POST["quantity"];
    $item_array = array(
        'id' => $_GET["id"],
        'item_name' => $_POST["hidden_name"],
        'product_price' => $_POST["hidden_price"],
        'item_quantity' => $_POST["quantity"],
    );
    $_SESSION["cart"][0] = $item_array;
 }
}

You need to take the quantity of the previous addition and add or subtract them together. 您需要取前一次添加的数量,然后将它们相加或相减。 You can also use $_SESSION["cart"] and store the ID as the index. 您也可以使用$_SESSION["cart"]并将ID存储为索引。

You should be able to use: (this isn't tested) 您应该可以使用:(未经测试)

if (isset($_POST["add"])){
    if(!isset($_SESSION["cart"][$_GET["id"]]['item_quantity'])){$_SESSION["cart"][$_GET["id"]]['item_quantity'] = 0;}
    $_SESSION["cart"][$_GET["id"]]['item_name'] = $_POST["hidden_name"];
    $_SESSION["cart"][$_GET["id"]]['product_price'] = $_POST["hidden_price"];
    $_SESSION["cart"][$_GET["id"]]['item_quantity'] = $_SESSION["cart"][$_GET["id"]]['item_quantity'] + $_POST["quantity"];

    header('Location: counter.php');
}
if (isset($_POST["decrease"])){
    if(!isset($_SESSION["cart"][$_GET["id"]]['item_quantity'])){$_SESSION["cart"][$_GET["id"]]['item_quantity'] = 0;}
    $_SESSION["cart"][$_GET["id"]]['item_name'] = $_POST["hidden_name"];
    $_SESSION["cart"][$_GET["id"]]['product_price'] = $_POST["hidden_price"];
    $_SESSION["cart"][$_GET["id"]]['item_quantity'] = ( ( ( $_SESSION["cart"][$_GET["id"]]['item_quantity'] - $_POST["quantity"] ) > 0  ) ? $_SESSION["cart"][$_GET["id"]]['item_quantity'] - $_POST["quantity"] : 0 );

    header('Location: counter.php');
}

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

相关问题 php增加和减少产品数量? - php increase and decrease quantity of product? 如何使用jquery提醒数量增加和数量减少值? - How to alert quantity increase and quantity decrease value using jquery? 如果增加和减少数量,我如何求和? - How i can sum quantity if increase and decrease quantity? 如何在数量框中添加增加和减少按钮 - How to add a increase and decrease button to quantity box 当 woocommerce 中的产品数量增加/减少时,如何从商店页面更新购物车? - How to update cart from shop page when product quantity increase/decrease in woocommerce? 如果会话中已有产品,如何增加产品数量 - How to increase product quantity if a product is already exist in session 如何使用PHP MySQL或javascript增加/删除产品数量。 或如何插入产品的数量值和复选框 - how to increase/delete quantity of a product in PHP MySQL or javascript. Or how to insert a quantity value of a product along with a checkbox woocommerce 与 ajax 的数量增加/减少? - quantity increase/decrease in woocommerce with ajax? laravel session购物车-如果产品已经存在,如何增加数量 - laravel session shopping cart - how to increase the quantity if the product already exists 如何增加购物车中的物品数量 - How to increase Items Quantity in Cart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM