PHP的"while"循环还是另一个"while"循环中的"if"语句? [英] PHP 'while' loop or 'if' statement in another 'while' loop?

查看:81
本文介绍了PHP的"while"循环还是另一个"while"循环中的"if"语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回HTML表的PHP/MySQL查询,但我陷入了需要在该查询中进行第二个while循环的部分.我不确定从这里要去哪里.我尝试了几种不同的方法.

I have a PHP/MySQL query that returns to an HTML table, and I'm stuck on a part where I need to make a second while loop in that query. I'm not sure where to go from here. I've tried a couple of different ways.

我希望它循环并给我第一组数据,然后使用"Order_ID"获得第二组数据并将第二组数据放入第一循环,然后再次执行.

I want it to loop and give me the first set of data, then use the "Order_ID" and get a second set of data and put that second set in the first loop, then do it again.

这就是我所拥有的...

Here's what I have...

<?php
    $arrayLC = array();

    $OrdersToShip = mysql_query("
        SELECT *
        FROM Orders o
        WHERE o.LotCoded = 0 ORDER BY o.Order_ID");

    if ($OrdersToShip) {
        while ($info = mysql_fetch_array($OrdersToShip))
        {
            $Order_ID = $info['Order_ID'];
            $Customer_ID = $info['Customer_ID'];
            $OrderDate = $info['OrderDate'];

            $lotCodes = mysql_query("SELECT lotCode, Qty FROM `OrdersLotCodes` WHERE `Order_ID` = '".$Order_ID."'");

            if($lotCodes) {
                while ($info = mysql_fetch_array($lotCodes))
                {
                    $lotCode = $info['lotCode'];
                    $Qty = $info['Qty'];
                    array_push($arrayLC, $lotCode, $Qty);
                }
            }

            echo '<tr class="OLine">
                 <td><input type="button" class="button viewThis" value="VIEW"></td>
                 <td>'.$Order_ID.'</td>
                 <td>'.$Customer_ID.'</td>
                 <td>'.$OrderDate.'</td>
                 <td>'.print_r($arrayLC).'</td>
                 </tr>';
        }
    }
    else {
        echo "encountered an error.".mysql_error();
    }
    mysql_close($conn);
?>

我想念什么?我该怎么办?

What am I missing? What should I do?

:: EDIT ::

:::

我已将mysql_query更改为:

SELECT o.Order_ID, o.Customer_ID, o.OrderDate, olc.lotCode, olc.qty
FROM Orders o
LEFT JOIN OrdersLotCodes olc ON o.Order_ID = olc.Order_ID
WHERE o.LotCoded = 0 ORDER BY o.Order_ID

现在,我如何将输出与OrderLotCodes一起使用,并将它们放入要在表中打印的数组中?我如何将它们放入数组,然后通过Order_ID带入相关的数组?

Now, how would I take the output with the OrderLotCodes and put them into an array to be printed in the table? How would I put them in an array then bring the related one by Order_ID?

推荐答案

我看到的一个问题是,您没有在while外部循环中重置$arrayLC 内部;因此,每个订单的代码都会无限地附加到前一个订单的代码中.

One problem that I can see is that you are not resetting $arrayLC inside the outer while loop; therefore, the codes from each order get appended to those of the previous one, ad infinitum.

if($OrdersToShip) {
    while ($info = mysql_fetch_array($OrdersToShip)) {

    $arrayLC = array(); // SHOULD BE IN HERE!

除此之外,当您使此代码正常工作时,您应考虑以下事实:对于N个订单,此代码执行N + 1个查询(1个获取订单,每个订单获取一个).考虑到如果执行 SQL联接以及它们如何帮助您检索关联的数据.

Apart from that, when you get this code working you should think about the fact that for N orders, this code executes N + 1 queries (1 to get the orders and one per order to get the items). This is a really inefficient way to do things considering that you can retrieve the same information with just one query if you perform a left outer join. So your next stop should be reading up on SQL joins and how they can help you retrieve associated data.

这篇关于PHP的"while"循环还是另一个"while"循环中的"if"语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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