更新购物车中一种产品的数量会重置其他产品先前更新的数量

我设法使用下拉数量菜单更改我的购物车中一种产品的数量,但是this.form.submit()如果我在购物车中有两种具有更新数量的产品并且我决定再次更新其中一种,另一种产品的数量将被覆盖并且恢复到其之前的原始值(更新前),该值是使用列出所有产品的页面上的“添加到购物车”发送的。


当我更新一个产品的数量时,如何保留其他产品已经更新的数量?


<?php

// Add products to basket:


if (!isset($_SESSION['basket'])) {

  $_SESSION['basket'] = array();

}


if (isset($_POST['id'])) {

  $_SESSION['basket'][$_POST['id']] = array(

    'product_id'=>$_POST['id'],

    'product_photo'=>$_POST['hidden_photo'],

    'product_photo_alt'=>$_POST['hidden_photo_alt'],

    'product_name'=>$_POST['hidden_name'],

    'product_price'=>$_POST['hidden_price'],

    'product_quantity'=>$_POST['quantity'],

  );

}

?>


<?php

// 

 if(!empty($_SESSION['basket'])) {

   $total = 0;

   foreach ($_SESSION['basket'] as $key => $value) {

// Below is the code which updates the quantity of a product with a particular id

// But after submitting the form resetes the quantity of other products 

// With already updated quantity 

     if (!empty($_POST['new_qty'][$value['product_id']])) {

       $quantity = $_POST['new_qty'][$value['product_id']];

     } else {

       $quantity = $value['product_quantity'];

     }

?>


<form method="post" action="">

<select class="quantity-basket" name="new_qty[<?php echo $value['product_id']; ?>]" onchange="this.form.submit()">


<?php

// Quantity added to the basket:                        

if ($quantity) {

?>


<option value="<?php echo $quantity; ?>" hidden><?php echo $quantity; ?></option>


<?php

}

?>


<?php

// Drop-down quantity menu

for ($i = 0; $i <=50; $i++) {

?>


<option value="<?php echo ($i == 0) ? $i . ' (Remove)' : $i; ?>">


<?php echo ($i == 0) ? $i . ' (Remove)' : $i; ?>


</option>


<?php

}

?>


</select>

<!-- End of quantity drop-down menu -->

</form>

http://img1.mukewang.com/643a07b90001023b06540454.jpg

慕森王
浏览 106回答 1
1回答

湖上湖

<?phpif (!isset($_SESSION['basket'])) {  $_SESSION['basket'] = array();}if (isset($_POST['id'])) {  $_SESSION['basket'][$_POST['id']] = array(    'product_id'=>$_POST['id'],    'product_photo'=>$_POST['hidden_photo'],    'product_photo_alt'=>$_POST['hidden_photo_alt'],    'product_name'=>$_POST['hidden_name'],    'product_price'=>$_POST['hidden_price'],    'product_quantity'=>$_POST['quantity'],  );}// Update quantityif (isset($_POST['action']) && $_POST['action'] == 'update') {  foreach($_SESSION['basket'] as &$value) {    if($value['product_id'] === $_POST['type']) {        $value['product_quantity'] = $_POST['new_qty'];        break; // Stop the loop after we've found the product    }  }}?><?php//  if(!empty($_SESSION['basket'])) {   $total = 0;   foreach ($_SESSION['basket'] as $product) {?><form method="post" action=""><input type="hidden" name="type" value='<?php echo $product["product_id"]; ?>'><input type="hidden" name="action" value="update"><select class="quantity-basket" name="new_qty" onchange="this.form.submit()"><option value="<?php echo $product['product_quantity']; ?>" hidden><?php echo $product['product_quantity']; ?></option><?php// Drop-down quantity menufor ($i = 0; $i <=50; $i++) {?><option value="<?php echo ($i == 0) ? $i . ' (Remove)' : $i; ?>"><?php echo ($i == 0) ? $i . ' (Remove)' : $i; ?></option><?php}?></select></form>
打开App,查看更多内容
随时随地看视频慕课网APP