2012-11-08 68 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; 
      } 
     } 

其余的代码...