花了很长时间,但我有一个购物车工作。问题是,在我的业务性质中,客户的购物车中可能有数百件商品,而我更新单个商品的方式非常原始,如果有很多商品需要更新,可能会变得乏味。
对于客户购物车的购物车数据库中的每一行,我都会生成商品的表单和产品信息。因此,每一行都有它自己的更新项目按钮。
$top_query = "SELECT t1.product_id, image_path, suggested_quantity, sales_info sku_item_number FROM product_customer_suggested_qty AS t1
LEFT JOIN product_images AS t2 ON t1.product_id = t2.product_id
RIGHT JOIN products AS t3 ON t1.product_id = t3.product_id
WHERE customer_id = '".$customer_id."'
AND cart_visible != 0";
//echo $top_query;
$histresult = mysql_query($top_query) or die (mysql_error());
<tr>
<td width="181"><strong>Product</strong></td>
<td width="181"><strong>Sales Info</strong></td>
<td width="100"><strong>Suggested Qty</strong></td>
<td width="100"><strong>Qty</strong></td>
<td width="100"><strong>Image</strong></td>
<td width="100"><strong>Add to Cart?</strong></td>
</tr>
<?php
$i =0;
while($row=mysql_fetch_array($histresult))
{
echo '<tr height = "50px">';
//We grab the product id as well as everything else we need.
$customer_id= $row['customer_id'];
$product_id= $row['product_id'];
$link = $row['image_path'];
$sku_item_number = $row['sku_item_number'];
$suggested_quantity = $row['suggested_quantity'];
echo '
<form action="ezcopy.php? method="post"><td>'.$sku_item_number.'</td>';
echo '<td>'.$suggested_quantity.'
<input class="same" id="same'.$i.'" title="'.$i.'" name="same'.$i.'" type="checkbox" value ="'.$suggested_quantity.'"/>
<input name="customer_id" type="hidden" value="'.$customer_id.'">
<input name="product_id" type="hidden" value="'.$product_id.'">
<input name="i" type="hidden" value="'.$i.'">
</td>';
echo '<td><input class="qty" name="qty'.$i.'" id="qty'.$i.'" type="text"size="4" maxlength="4"></td>';
这只是整行的一部分,它完全可以用于更新数量,并允许用户根据他们的历史记录自动复制建议的值。
但是,如果我只想在底部制作一个显示“全部更新”的按钮,从而使整个购物车成为一个大表格,我该如何修改数量表格,以便它可以读取未知的动态数量的项目并更新数据库?实现这一目标的好策略是什么?