猿问

如何在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>';


将一件商品添加到购物车,并且可以多次添加一件商品,但是当我尝试添加不同的商品时,没有添加,而是更新了上一件商品的数量,如下图所示

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

         

智慧大石
浏览 162回答 1
1回答

有只小跳蛙

也许$ _POST ['id']总是一样的。
随时随地看视频慕课网APP
我要回答