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

时间:2019-02-13 08:43:56

标签: php mysqli

我遇到的问题是,当我第二次单击添加按钮时,它无法增加,而且我不知道如何执行减少按钮。

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>

第一次单击仅显示添加的输出,但是第二次单击将显示“产品已添加到列表”,不能增加产品数量。实际结果应该是数量增加。减少产品数量的相同问题

2 个答案:

答案 0 :(得分:0)

您必须更改增加和减少数量的逻辑。

如果按添加按钮,则将乘积的值增加1,反之亦然。因此,首先需要获取当前添加产品的数量,然后由操作员添加或删除。

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

下面给出的基本思想。

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

答案 1 :(得分:0)

您需要取上一个加法的数量并将它们加或减。 您还可以使用$_SESSION["cart"]并将ID存储为索引。

您应该可以使用:(未经测试)

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