【问题标题】:while($row = mysql_fetch_assoc($result)) - How to foreach $row?while($row = mysql_fetch_assoc($result)) - 如何foreach $row?
【发布时间】:2023-03-24 04:04:02
【问题描述】:

我正在开发一个简单的订单系统。

我坚持的代码如下:

if (isset($_GET['cart']))
{
$cart = array();
$total = 0;
foreach ($_SESSION['cart'] as $id)
{
    foreach ($items as $product)
    {
        if ($product['id'] == $id)
        {
            $cart[] = $product;
            $total += $product['price'];
            break;
        }
    }
}

include 'cart.html.php';
exit();
}

这是基于预设数组构建的代码。我正在使用 mysql 中包含几列的表。

我决定如下:

if (isset($_GET['cart']))
{
$cart = array();
$total = 0;
foreach ($_SESSION['cart'] as $id)
{
            while($row = mysql_fetch_assoc($productsSql)) {
    foreach ($row as $product)
    {
        if ($product['id'] == $id)
        {
            $cart[] = $product;
            $total += $product['price'];
            break;
        }
    }
}
    include 'cart.html.php';
exit();
}}

为了展示这个“购物车”,我决定这样做:

foreach ($cart as $item) {
        $pageContent .= '
                <tr>
                    <td>'.$item['desc'].'</td>
                    <td>
                        R'.number_format($item['price'], 2).'
                    </td>
                </tr>
';

    }

所有这一切似乎都是以一种方式生成我的购物车,当查看它时,它会显示一个仅包含项目 id 的列表,例如描述和价格应该在哪里,我只得到两个字段中的项目 id...我也得到总价格 0。

谁能发现我哪里出错了?

或者至少尝试给我一些意见,以便我朝着正确的方向前进!

谢谢!!

$productsQuery = 'SELECT `id`, `refCode`, `desc`, `pack`, `measure`, `quantity`, `deptCode`, `taxable`, `price1`, `price2`, `crdCode`, `cost1`, `cost2` FROM `products` ORDER BY `desc` ';
$productsSql = mysql_query($productsQuery) or die(mysql_error());
if (mysql_num_rows($productsSql) == 0) {
die('No results.');
} else {
$orderContent = '';
while($row = mysql_fetch_assoc($productsSql)) {
$prId = $row['id'];
$prRefCode = $row['refCode'];
$prDesc = $row['desc'];
$prPack = $row['pack'];
$prMeasure = $row['measure'];
$prQuantity = $row['quantity'];
$prDeptCode = $row['deptCode'];
$prTaxable = $row['taxable'];
$prPrice1 = $row['price1'];
$prPrice2 = $row['price2'];
$prCrdCode = $row['crdCode'];
$prCost1 = $row['cost1'];
$prCost2 = $row['cost2'];
$orderContent .= '
    <tr>
        <td>'.$prId.'</td>
        <td>'.$prDesc.'</td>
        <td>'.$prPack.'x'.$prSize.' '.$prMeasure.'</td>
        <td>R'.$prPrice1.'</td>
        <td>
            <form action="" method="post">
                <div>
                    <input type="text" size="3" name="quantity" /> 
                </div>
            </form>
        </td>
        <td>
            <form action="" method="post">
                <div>
                    <input type="hidden" name="id" value="'.$prId.'" />
                    <input type="submit" name="action" value="Order" />
                </div>
            </form>
        </td>           
   </tr>
';
}}

【问题讨论】:

  • @yokoloko 在我的帖子中查看编辑

标签: php mysql foreach row


【解决方案1】:

你的中间代码块没有意义。您遍历会话变量,每次都运行一个查询......但它是相同的查询,并使用未定义的变量来启动。遍历结果行没有多大意义,因为您将遍历查询为每一行返回的各个字段。

例如如果您的 products 表只有 (id, name, description) 作为字段,那么 $row 将是一个如下所示的数组:

$row = array('id' => xxx, 'name' => yyy, 'description' => zzz);

当您在$row()foreach() 时,您将不会获得单个产品,而是会在行中获得这些字段。 $product 将是 xxx,然后是 yyy,然后是 zzz

我不知道您的代码要完成什么 - 我猜您正在尝试检索用户添加到购物车中的产品的价格,但您正在以一种非常奇怪的方式进行操作非常低效的方式。

【讨论】:

  • 感谢您的输入,是的,这对我来说也很奇怪和困惑。我想要完成的是有一个产品目录及其价格。用户可以通过单击相应行上的订单来选择产品。查看“购物车”时,它将显示已订购商品的列表,以及基于添加到购物车中的每件商品的总价格的总和。完成后,订单将通过电子邮件发送。我只是在这里头脑风暴:)
【解决方案2】:

假设您的数据库中的每一行看起来都像这样......

[product_id][product_name][product_description][product_price]

当您使用 while 循环将查询返回分配给通过 mysql_fetch_assoc() 传递的变量时,每次传递都会隔离一整行。您可以通过数组键引用 ($array['product_id']) 或使用 foreach 循环手动拆分其中。我认为您遇到的问题是您将其混淆了。牢记上面的表格布局示例,您可以执行以下操作:

while ($tableRow = mysql_fetch_assoc($query)) { // Loops 3 times if there are 3 returned rows... etc

    foreach ($tableRow as $key => $value) { // Loops 4 times because there are 4 columns
        echo $value;
        echo $tableRow[$key]; // Same output as previous line
    }
    echo $tableRow['product_id']; // Echos 3 times each row's product_id value
}

查看代码中的这一行:if ($product['id'] == $id) { }

我想你可能指的是if ($row['id'] == $id) { }

【讨论】:

    猜你喜欢
    • 2012-05-18
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 2023-02-16
    • 2019-04-03
    相关资源
    最近更新 更多