简体   繁体   English

PHP的“ while”循环还是另一个“ while”循环中的“ if”语句?

[英]PHP 'while' loop or 'if' statement in another 'while' loop?

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. 我有一个返回HTML表的PHP / MySQL查询,但我陷入了需要在该查询中进行第二次while循环的部分。 I'm not sure where to go from here. 我不确定从这里去哪里。 I've tried a couple of different ways. 我尝试了几种不同的方法。

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. 我希望它循环并给我第一组数据,然后使用“ Order_ID”并获得第二组数据并将第二组数据放入第一个循环,然后再次执行。

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:: ::编辑::

I've changed the mysql_query to: 我已经将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

Now, how would I take the output with the OrderLotCodes and put them into an array to be printed in the table? 现在,如何将输出与OrderLotCodes一起放入一个要打印在表中的数组中? How would I put them in an array then bring the related one by Order_ID? 我如何将它们放入数组,然后通过Order_ID带入相关的数组?

One problem that I can see is that you are not resetting $arrayLC inside the outer while loop; 我可以看到的一个问题是您没有在外部while循环重置$arrayLC 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!

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). 除此之外,当您使该代码正常工作时,您应考虑以下事实:对于N个订单,此代码执行N + 1个查询(1个获取订单,每个订单获取一个)。 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. 因此,您的下一站应该是阅读SQL连接以及它们如何帮助您检索关联的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM