while($ row = mysql_fetch_assoc($ result)) - 如何foreach $ row? [英] while($row = mysql_fetch_assoc($result)) - How to foreach $row?

查看:252
本文介绍了while($ row = mysql_fetch_assoc($ result)) - 如何foreach $ row?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b $我是一个简单的订单系统。 b

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

包含'cart.html.php';
exit();
}

这是代码是建立在预设数组上的。我正在使用一个在mysql中有几列的表。



我已经决定了以下内容:

<$ p如果(isset($ _ GET ['cart']))
{
$ cart = array(); $ p $
$ 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;
}
}
}
包含'cart.html.php';
exit();
}}

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

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






所有这些似乎都是以一种时尚在查看时显示只有项目的id列表,例如在那里的描述和价格应该是,我只得到在这两个领域的物品编号...我也得到了总价格为0



任何人都可以在哪里我在这里错了?



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



$ p
$ b $ $ $ $ $ $ $ $ $ $ {code $ $ ```````量``,'量',`deptCode`,`taxable`,`price1`,`price2`,`crdCode`,`cost1`,`cost2`从`products` ORDER BY`desc`;
$ productsSql = mysql_query($ productsQuery)或死(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 =textsize =3name =quantity/>
< / div>
< / form>
< / td>
< td>
< form action =method =post>
< div>
< input type =hiddenname =idvalue ='。$ prId。'/>
< input type =submitname =actionvalue =Order/>
< / div>
< / form>
< / td>
< / tr>
';
}}


解决方案

您的数据库中的行看起来像这样...

pre code $ {product_id] [product_name] [product_description] [product_price]

当您将查询返回给通过 mysql_fetch_assoc()使用while循环,每一遍都会隔离整行。你可以通过数组键引用( $ array ['product_id'] )或者使用foreach循环来手工拆分。我认为你遇到的问题是你正在混合起来。记住上面的表格布局,你可以做如下的事情:

  while($ tableRow = mysql_fetch_assoc($ query )){//如果有3个返回行,则循环3次... etc 

foreach($ tableRow as $ key => $ value){//循环4次,因为有4列
echo $ value;
echo $ tableRow [$ key]; //与上一行相同的输出
}
echo $ tableRow ['product_id']; // Echos每行3次product_id的值
}

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


I am working on a simple order system.

the peice of code I am stuck on is the following:

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();
}

This is the code is build on a preset array. I am working with a table with a few columns in mysql.

I have decided on the following:

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();
}}

To display this "cart" I have decided on this:

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

    }

All this seems to do is produce my cart in a fashion where when viewed it displays a list of only the items' id, e.g. where the description and price are supposed to be, I only get the items id in both fields... I also get a total price of 0.

Can anyone spot where I am going wrong here?

Or atleast try to give me some input so that I get going in the right direction!

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

解决方案

Let's say that each of the rows in your database looks like this...

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

When you assign your query return to a variable passed through mysql_fetch_assoc() using a while loop, each pass will isolate a whole row. Of which you can piece apart manually by array key reference ($array['product_id']) or by using a foreach loop. I think the problem you're having is that you are mixing this up. Keeping the above example table layout in mind, you could do something like the following:

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
}

Look at this line in your code: if ($product['id'] == $id) { }

I think you probably mean if ($row['id'] == $id) { } instead.

这篇关于while($ row = mysql_fetch_assoc($ result)) - 如何foreach $ row?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆