while($row = mysql_fetch_assoc($result)) - 如何 foreach $row?

2023-11-22

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

我所坚持的代码如下:

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 的总价。

谁能发现我在这里出错的地方吗?

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

Thanks!!

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

假设数据库中的每一行都是这样的......

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

当您将查询返回分配给传递的变量时mysql_fetch_assoc()使用 while 循环,每次传递都会隔离整行。您可以通过数组键引用手动拆分其中($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) { }反而。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

while($row = mysql_fetch_assoc($result)) - 如何 foreach $row? 的相关文章

随机推荐