如何更改购物车从更新一个项目到更新所有项目

时间:2012-08-18 20:53:33

标签: php html forms dynamic cart

花了很长时间,但我买了一个购物车。问题是,就我的业务而言,客户可以在他们的购物车中有数百种东西,我更新单个商品的方式非常原始,如果有很多商品需要更新,可能会变得乏味。

对于客户购物车的购物车数据库中的每一行,我生成商品的表单和产品信息。因此每行都有自己的更新项按钮。

$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>';

这只是整行的一部分,它完全适用于更新数量,并允许用户根据历史记录自动复制建议值。

但是,如果我只想在底部只显示一个“全部更新”按钮,从而使整个购物车成为一个大型表格,我该如何修改数量表单以便它可以读取未知的,动态的项目数量和更新数据库?实现这一目标的好策略是什么?

1 个答案:

答案 0 :(得分:0)

经过与Google的挣扎,我终于在这里找到了一个例子。 http://www.theblog.ca/update-multiple-rows-mysql

关键是在行生成循环之外将表单更改为一个大表单而不是许多小表单。

 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'];
                $sales_info = $row['sales_info'];
                $item_id = $row['id_product_customer_suggested_qty'];

                 echo '<td>'.$sku_item_number;
                 //This array stores the item index.

                 echo '</td>';

                 echo '<td>'.$sales_info.'</td>';
    echo '<td>'.$suggested_quantity.'
    <input class="same" id="same'.$i.'" title="'.$i.'" name="same'.$i.'" type="checkbox" value ="'.$suggested_quantity.'"/> 
    <input name="item_id[]" type="hidden" value="'.$item_id.'">
    <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[]" id="qty'.$i.'" type="text"size="4" maxlength="4"></td>';

第一个关键是输入名称的更改方式。它们现在都是阵列。行生成循环结束时是一个提交按钮。然后,提交时发生的是下一个键。

foreach($_POST['item_id'] as $key=>$item_id)
{ 
    $qty = $_POST['qty'][$key];
    $product_id = $_POST['product_id'][$key];
    $customer_id = $_POST['customer_id'][$key];

    if (!empty($qty))
    {
        echo 'Product is '.$product_id.'<br>';
        echo 'Quantity requestesd is '.$qty.'<br><br>';
        //perform UPDATE query as that inidividual item
        }
}

这会动态地抓取表单中的每个现有行,每个行都由item_id设置为唯一。我们现在可以根据需要执行更新查询。这是一个非常强大的技巧。