我在 PHP 文件中創(chuàng)建了一個(gè)簡(jiǎn)單的購(gòu)物車,我為此使用了會(huì)話,我能夠完美地顯示訂單點(diǎn)擊并顯示在 cart.ph 中?,F(xiàn)在,我為客戶在購(gòu)物車頁(yè)面中訂購(gòu)的每件商品添加了一個(gè)數(shù)量文本字段?,F(xiàn)在我想要摘要頁(yè)面,其中包含客戶輸入的產(chǎn)品名稱和數(shù)量,但顯然我只能在購(gòu)物車中獲得一種具有正確數(shù)量的產(chǎn)品,其余的默認(rèn)為 1 這是我的代碼這是我的購(gòu)物車。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,此文件將從購(gòu)物車中獲取所有產(chǎn)品以及客戶/用戶編輯的數(shù)量,但顯然我只能基于 cart.php 顯示 1 個(gè)正確的數(shù)量記錄
1 回答

哆啦的時(shí)光機(jī)
TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊
我建議您將所有這些都保存在數(shù)據(jù)庫(kù)中。
創(chuàng)建carts
表并在其中添加所需的所有字段,并在 id make string 之后的開(kāi)頭添加uuid
。
對(duì)于訪問(wèn)您網(wǎng)站的每個(gè)客戶,創(chuàng)建uuid
并將其放入 cookie 中,并且在將用戶添加到購(gòu)物籃時(shí),使用該 在數(shù)據(jù)庫(kù)中創(chuàng)建一個(gè)條目uuid
。uuid
將是用戶 ID。如果您有登錄信息,請(qǐng)使用user_id
。然后如果有用戶登錄則從數(shù)據(jù)庫(kù) byuuid
或 by輸出。user_id
顯示數(shù)據(jù)庫(kù)中的記錄比填寫服務(wù)器會(huì)話更方便
- 1 回答
- 0 關(guān)注
- 154 瀏覽
添加回答
舉報(bào)
0/150
提交
取消