Hướng dẫn update cart item in php - cập nhật mặt hàng giỏ hàng trong php

Tôi đang cố gắng tìm ra cách cập nhật giỏ hàng của mình mà không cập nhật các sản phẩm khác.Tôi có nên làm việc với các phiên ở đây hay không?Vấn đề hiện tại của tôi là bất cứ khi nào tôi thay đổi số lượng, nó cũng cập nhật các sản phẩm khác và đặt số lượng trong hộp trở lại 1. Làm thế nào tôi có thể thay đổi điều này?

Đây là những gì tôi hiện có, tôi hiểu tại sao nó cập nhật tất cả các sản phẩm nhưng tôi không thể tìm ra cách làm điều đó khác.

<?php
session_start();

include("header.php");
include_once("dbconnect.php");
include("functies.php");

if (empty($_SESSION['cart'])) {
    echo "U heeft geen producten in uw winkelwagen";

} else { 

$items = $_SESSION['cart'];
$cartitems = explode(",", $items);

?> 

<div align="center">
<?php titel(); ?>
<h2 Winkelwagen <h2>
</div>

<div class="container">
    <div class="row">
        <div class="col-sm-12 col-md-10 col-md-offset-1">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Quantity</th>
                        <th class="text-center">Price</th>
                        <th class="text-center">Total</th>
                        <th> </th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        $total = 0;
                        $i=1;

                        foreach ($cartitems as $key=>$id) { 
                            $sql = "SELECT * FROM products WHERE id = $id";
                            $res=mysqli_query($conn, $sql);
                            $r = mysqli_fetch_assoc($res);

                            $sqlb = "SELECT * FROM brewery WHERE code = $r[brewery_code]";
                            $resb=mysqli_query($conn, $sqlb);
                            $rb = mysqli_fetch_assoc($resb);

                            if (isset($_POST['submit'])) {
                                $amount = $_POST['amount'];
                            } else $amount = 1;
                        ?>  
                    <tr>
                        <td class="col-sm-8 col-md-6">
                        <div class="media">
                            <img class="thumbnail pull-left" src="Images/<?php echo $r['name'] ?>.jpg" width="85" height="152" alt="..."  >
                            <div class="media-body">
                                <h4 class="media-heading"><?php echo $r['name']; ?></h4>
                                <h5 class="media-heading"> by <?php echo $rb['name'];; ?></a></h5>
                                <span>Status: </span><span class="text-success"><strong>In Stock</strong></span>
                            </div>
                        </div></td>
                        <td class="col-sm-1 col-md-1" style="text-align: center">
                        <form action="" method="post">
                        <input type="number" class="form-control" name="amount" value="1" min="1">
                        <input type="submit" name="submit" class="btn btn-primary btn-sm">
                        </form>
                        </td>
                        <?php 
                             $total = $total + $r['price'];
                             $i++;  
                             $producttotal = $amount * $r['price'];
                            ?>
                        <td class="col-sm-1 col-md-1 text-center"><strong>€ <?php echo number_format($r['price'],2);?> </strong></td>
                        <td class="col-sm-1 col-md-1 text-center"><strong>€ <?php echo number_format($producttotal,2);?> </strong></td>
                        <td class="col-sm-1 col-md-1">
                        <button type="button" class="btn btn-danger">
                            <a href="delcart.php?remove=<?php echo $key; ?>">Verwijderen</a>
                        </button></td>
                    </tr>

                        <?php } ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td>   </td>
                        <td>   </td>
                        <td>   </td>
                        <td><h5>Subtotal<br>Estimated shipping</h5><h3>Total</h3></td>
                        <td class="text-right"><h5><strong>$24.59<br>$6.94</strong></h5><h3>$31.53</h3></td>
                    </tr>
                    <tr>
                        <td>   </td>
                        <td>   </td>
                        <td>   </td>
                        <td>
                        <button type="button" class="btn btn-primary"> Continue Shopping </button></td>
                        <td>
                        <button type="button" class="btn btn-primary"> Checkout  </button>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>

                        <?php } ?>

Đã hỏi ngày 7 tháng 11 năm 2017 lúc 11:48Nov 7, 2017 at 11:48

Hướng dẫn update cart item in php - cập nhật mặt hàng giỏ hàng trong php

0

Biểu mẫu của bạn cần chứa một chỉ số cho sản phẩm bạn muốn tăng số lượng.Ví dụ, như thế này:

 <form action="" method="post">
       <input type="number" class="form-control" name="amount[<?php echo $id;?>]" value="1" min="1">
       <input type="submit" name="submit" class="btn btn-primary btn-sm">
 </form>

Sau đó, bạn có thể đánh giá ID như thế này:

if (isset($_POST['submit']) && isset($_POST['amount'][$id])) {
    $amount = $_POST['amount'][$id];
} else { 
    $amount = 1;
}

Tôi không hiểu tại sao bạn sẽ đặt số tiền thành 1 nếu nó không được đặt trong $ _POST.Tôi nghĩ rằng bạn phải lưu trữ số tiền cho mỗi sản phẩm trong phiên, không chỉ ID như bạn đang làm bây giờ.

Có thể như thế này:

   if (isset($_POST['submit']) && isset($_POST['amount'][$id])) {
        $amount = intval($_POST['amount'][$id]);
    } else { 
        if(isset($_SESSION['amounts'][$id])) {
            $amount = $_SESSION['amounts'][$id];
        } else { 
            $amount = 1;
        }
    }
    if(!isset($_SESSION['amounts'])) $_SESSION['amounts'] = array();
    $_SESSION['amounts'][$id] = $amount;

Đã trả lời ngày 7 tháng 11 năm 2017 lúc 12:07Nov 7, 2017 at 12:07

Hướng dẫn update cart item in php - cập nhật mặt hàng giỏ hàng trong php

AdderadderAdder

5.6431 Huy hiệu vàng23 Huy hiệu bạc52 Huy hiệu đồng1 gold badge23 silver badges52 bronze badges

0