我在 PHP 文件中創建了一個簡單的購物車,我為此使用了會話,我能夠完美地顯示訂單點擊并顯示在 cart.ph 中?,F在,我為客戶在購物車頁面中訂購的每件商品添加了一個數量文本字段?,F在我想要摘要頁面,其中包含客戶輸入的產品名稱和數量,但顯然我只能在購物車中獲得一種具有正確數量的產品,其余的默認為 1 這是我的代碼這是我的購物車。ph<?phpsession_start();$status="";if (isset($_POST['action']) && $_POST['action']=="remove"){if(!empty($_SESSION["shopping_cart"])) { foreach($_SESSION["shopping_cart"] as $key => $value) { if($_POST["code"] == $key){ unset($_SESSION["shopping_cart"][$key]); $status = "<div class='box' style='color:red;'> Product is removed from your cart!</div>"; } if(empty($_SESSION["shopping_cart"])) unset($_SESSION["shopping_cart"]); } }}if (isset($_POST['action']) && $_POST['action']=="change"){ foreach($_SESSION["shopping_cart"] as &$value){ if($value['code'] === $_POST["code"]){ $value['quantity'] = $_POST["quantity"]; break; // Stop the loop after we've found the product }} }?><html><head><title>SKIP-CART</title><link rel='stylesheet' href='css/style.css' type='text/css' media='all' /></head><body><div style="width:700px; margin:50 auto;"><h2>Shopping Cart</h2> <?phpif(!empty($_SESSION["shopping_cart"])) {$cart_count = count(array_keys($_SESSION["shopping_cart"]));?><div class="cart_div"><a href="cart.php"><img src="cart-icon.png" /> Cart<span><?php echo $cart_count; ?></span></a></div><?php}?><div class="cart"><?phpif(isset($_SESSION["shopping_cart"])){ $total_price = 0;?> <table class="table"><tbody><tr><td></td><td>ITEM NAME</td><td>QUANTITY</td></tr> <?php foreach ($_SESSION["shopping_cart"] as $product){?>這是我的 display.php,此文件將從購物車中獲取所有產品以及客戶/用戶編輯的數量,但顯然我只能基于 cart.php 顯示 1 個正確的數量記錄
1 回答

哆啦的時光機
TA貢獻1779條經驗 獲得超6個贊
我建議您將所有這些都保存在數據庫中。
創建carts
表并在其中添加所需的所有字段,并在 id make string 之后的開頭添加uuid
。
對于訪問您網站的每個客戶,創建uuid
并將其放入 cookie 中,并且在將用戶添加到購物籃時,使用該 在數據庫中創建一個條目uuid
。uuid
將是用戶 ID。如果您有登錄信息,請使用user_id
。然后如果有用戶登錄則從數據庫 byuuid
或 by輸出。user_id
顯示數據庫中的記錄比填寫服務器會話更方便
- 1 回答
- 0 關注
- 158 瀏覽
添加回答
舉報
0/150
提交
取消