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

Cra*_*der 4 php mysql foreach row

我正在研究一个简单的订单系统.

我坚持的代码是如下:

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();
}
Run Code Online (Sandbox Code Playgroud)

这是代码构建在预设数组上.我正在使用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();
}}
Run Code Online (Sandbox Code Playgroud)

要显示这个"购物车",我决定:

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

    }
Run Code Online (Sandbox Code Playgroud)

所有这一切似乎都是以一种方式生产我的购物车,在查看它时只显示项目的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>
';
}}
Run Code Online (Sandbox Code Playgroud)

65F*_*f05 14

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

[product_id][product_name][product_description][product_price]
Run Code Online (Sandbox Code Playgroud)

当您将查询返回给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
}
Run Code Online (Sandbox Code Playgroud)

在代码中查看以下行: if ($product['id'] == $id) { }

我想你可能意味着if ($row['id'] == $id) { }.