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

我已经在php中创建了一个购物车,当用户单击“添加到购物车”按钮将用户重定向到购物车页面时,该商品会添加到购物车中。购物车会多次添加1个产品,但是当我添加另一个产品时,它会增加购物车中旧产品的数量,而不是显示2个产品。以下是我的cart.php页面


<?php


// Start Session

session_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>

   

一项添加到购物车,并且第一项可以添加多次,但是当我尝试添加其他项时,没有添加,而是更新前一项的数量,如下图所示


http://img3.mukewang.com/60a637330001893408180204.jpg

我需要列出用户单击购物车中另一个下面单击的项目。谁能告诉我该怎么做?将有很大的帮助


千巷猫影
浏览 135回答 1
1回答

慕勒3428872

也许$ _POST ['id']总是一样的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript