2012-11-08 53 views
-2

下面的代碼將只更新我的購物車的最後一項。我在循環中掙扎了一下,我只是卡住了。上cart.html購物車 - 只更新1條目

if (isset($_POST['quantity'])) { // Update quantities in the cart 

    foreach ($_POST['quantity'] as $k => $qty) { //Full name on form: name="quantity[' . $row['prodid'] . ']" 

     $pid = $_POST ['prodid']; 
     $sp_type = $_POST ['cat']; 

     if (isset($sp_type, $pid)) { 
      // Determine the quantity: 
      $qty = (filter_var($qty, FILTER_VALIDATE_INT, array('min_range' => 0)) !== false) ? $qty : 1; 

      // Update the quantity in the cart: 
      $r = mysqli_query($dbc, "CALL update_cart('$uid', '$sp_type', $pid, $qty)"); 
     } 

    } // End of FOREACH 
} 

代碼:

這是爲HTML形式的購物車中的代碼:

<input type="hidden" name="prodid[' . $row['prodid'] . ']" value="' .$row ['prodid'] .'" /></td> 
     <td align="center"><input type="text" name="quantity[' . $row['prodid'] . ']" value="' . $row['quantity'] . '" size="2" class="small" /></td> 
     <td align="right">£' . $price . '</td> 
     <td align="right">£' . number_format($subtotal, 2) . '</td> 
     <td align="right"><a href="/wishlist.php?id=' . $row['prodid'] . '&action=move&qty=' . $row['quantity'] .'&cat=' .$row ['cat_id'] .'"> 
+0

你有數量上的循環,但不是的產品編號,我會重新安排我的表單元素的名稱更好地適應循環。例如:name =「quantity [$ prodid]」,所以這個數量的關鍵成爲產品編號 – Scuzzy

+0

你能否提供你的$ _POST params的var_dump? –

+0

我到底怎麼做?對不起,我是一個初學者(自學)。 – user1810442

回答

0

添加ID到表單中的所有的輸入之後。 我將以下代碼添加到上面的代碼,它的工作原理!

 foreach ($_POST['prodid'] as $id=>$prod){ 
      if ($id==$k){ 
       $pid = $prod; 
      } 
     } 
     foreach ($_POST['cat'] as $catid=>$cat){ 
      if ($catid==$k){ 
       $sp_type = $cat; 
      } 
     } 

其餘的代碼...