为了账号安全,请及时绑定邮箱和手机立即绑定

如何在php中的购物车中添加多个项目

如何在php中的购物车中添加多个项目

千巷猫影 2021-05-06 14:41:55
我已经在php中创建了一个购物车,当用户单击“添加到购物车”按钮将用户重定向到购物车页面时,该商品会添加到购物车中。购物车会多次添加1个产品,但是当我添加另一个产品时,它会增加购物车中旧产品的数量,而不是显示2个产品。以下是我的cart.php页面<?php// Start Sessionsession_start();// Application library ( with ShopingCart class )require __DIR__ . '/library.php';$app = new ShopingCart();if(isset($_POST['add_to_cart'])){    $app->addToCart($_POST['id']);}if (isset($_GET['id_to_remove']) && isset($_GET['id_to_remove']) != '') {    $app->removeProductFromCart($_GET['id_to_remove']);}?>                <?php                    if(isset($_SESSION['shopping_cart']) && count($_SESSION['shopping_cart']) > 0)                    {                        $entertainment = $_SESSION['shopping_cart'];                        echo '                                <table class="table table-hover table-bordered">                                <thead>                                    <tr>                                    <th scope="col">#</th>                                    <th scope="col">Title</th>                                    <th scope="col">Quantity</th>                                    <th scope="col">Price</th>                                    <th scope="col" width="100">Action</th>                                    </tr>                                </thead>';                        $item_number = 1;                        $total = 0;                        foreach ($entertainment as $product) {                        echo '                                <tbody>                                    <tr>                                    <th scope="row">'. $item_number .'</th>                                    <td>' . $product['title'] . '</td>                                    <td>'.$product['quantity'].'</td>   一项添加到购物车,并且第一项可以添加多次,但是当我尝试添加其他项时,没有添加,而是更新前一项的数量,如下图所示我需要列出用户单击购物车中另一个下面单击的项目。谁能告诉我该怎么做?将有很大的帮助
查看完整描述

1 回答

?
慕勒3428872

TA贡献1848条经验 获得超6个赞

也许$ _POST ['id']总是一样的。


查看完整回答
反对 回复 2021-05-20
  • 1 回答
  • 0 关注
  • 134 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信