代码之家  ›  专栏  ›  技术社区  ›  Ben J

php foreach循环仅返回数组中第一次迭代的值

  •  4
  • Ben J  · 技术社区  · 8 年前

    据我所知,阵列正在正确循环,正如您可以看到的那样 当前结果 在这里: https://i.imgur.com/JWD3nis.png 但奇怪的是ID:2没有生成表,ID 5创建了2个表,根据刚刚链接的imgur屏幕截图,都是空白的。

    我仔细检查了我的样本数据,每个表有5条唯一的记录,没有重复或问题。

    编辑:1 我忘了提及期望的结果,以澄清我希望循环如何工作。请查看此屏幕截图: https://i.imgur.com/4h7l49p.png

    编辑:2 https://pastebin.com/MG2gtASu 这是我的 如果有帮助: https://i.imgur.com/idVR5ev.png

    编辑:3 新的更新代码(谢谢Sand):

    <?php
    include('OrderCore/connect-db.php');
    $POIds = array();
    if ($result = $mysqli->query("SELECT ProductionOrderID FROM ProductionOrder" ) ) {
        while ($row = $result->fetch_object()) {
            $POIds[] = $row->ProductionOrderID;
        }
    }
    foreach ( $POIds as $index => $OrderId ) {
        if ( $result = $mysqli->query("
        SELECT * 
        FROM ProductionOrder AS p
        LEFT JOIN ProductionOrderStatus AS s ON ( p.ProductionOrderID = s.ProductionOrderStatusID ) 
        LEFT JOIN NotGood AS n ON ( p.ProductionOrderID = n.NGID ) 
        LEFT JOIN BatchOrder AS b ON ( p.ProductionOrderID = b.BatchID ) 
        LEFT JOIN Brand AS bd ON ( p.ProductionOrderID = bd.BrandID ) 
        LEFT JOIN CustomerOrder AS co ON ( p.ProductionOrderID = co.COID ) 
        LEFT JOIN Customer AS c ON ( p.ProductionOrderID = c.CustomerID ) 
        LEFT JOIN CustomerOrderStatus AS cos ON ( p.ProductionOrderID = cos.COStatusID ) 
        WHERE p.ProductionOrderID='$OrderId'") ) {
            while( $row = $result->fetch_object() ) {
                print "<h1>Order: $OrderId</h1>";
                print "<table class='table table-striped'>";
                print "<tr> <th>PO ID</th> <th>PO #</th> <th>Order Quantity</th> <th>Balance Left</th> <th>Production Date</th> <th>Production Order Status</th> <th>Not Good ID</th> </tr>";
                print "<td>" . $row->ProductionOrderID . "</td>";
                print "<td>" . $row->PONum . "</td>";
                print "<td>" . $row->OrderQTY . "</td>";
                print "<td>" . $row->BalLeftNum . "</td>";
                print "<td>" . $row->ProductionDate . "</td>";
                print "<td>" . $row->ProductionOrderStatusID . "</td>";
                print "<td>" . $row->NGID . "</td>";
                print "</tr>";
                print "</table>";
                //BatchOrder
                print "<table class='table table-striped'>";
                print "<tr> <th>Batch ID</th> <th>Brand Name</th> <th>Batch Quantity</th> <th>Availability Date</th> <th>Remaining Balance</th> <th>Production Order ID</th> </tr>";
                print "<td>" . $row->BatchID . "</td>";
                print "<td>" . $row->BrandID . "</td>";
                print "<td>" . $row->BatchQTY . "</td>";
                print "<td>" . $row->AvailDate . "</td>";
                print "<td>" . $row->RemainBal . "</td>";
                print "<td>" . $row->ProductionOrderID . "</td>";
                print "</tr>";
                print "</table>";
                //CustomerOrder
                print "<table class='table table-striped'>";
                print "<tr> <th>Customer ID</th> <th>Customer Name</th> <th>Invoice Quantity</th> <th>Invoice #</th> <th>Shipping Date</th> <th>Batch ID</th> <th>CO Status</th> </tr>";
                print "<td>" . $row->COID . "</td>";
                print "<td>" . $row->CustomerID . "</td>";
                print "<td>" . $row->InvoiceQTY . "</td>";
                print "<td>" . $row->InvoiceNum . "</td>";
                print "<td>" . $row->ShipDate . "</td>";
                print "<td>" . $row->BatchID . "</td>";
                print "<td>" . $row->COStatusID . "</td>";
                print "</tr>";
                print "</table>";
            }
        }
        else
        {
            print "No results to display!";
        }
    }
    $mysqli->close();
    ?>
    
    2 回复  |  直到 8 年前
        1
  •  3
  •   S4NDM4N    8 年前

    我知道为什么会这样,这是因为连接类型 INNER this

    发现了为什么只显示一批数据的问题。因为你在扳平比分 p.ProductionOrderID = b.BatchID 所以,查询会将生产ID与批次ID匹配,并且批次ID是唯一的,所以不会出现重复项,从而显示匹配行中的单个数据记录。您真正想要做的是匹配批次表中的生产ID,因为这是生产表和批次表之间的关系。现在,当您运行此操作时,它将绘制表格,直到批处理结束。

    while foreach 你不需要另一个 SQL $row["BatchQTY"]

    这是我的解决方案。

    if ( $result = $mysqli->query("
        SELECT * 
    FROM ProductionOrder AS p
    LEFT JOIN ProductionOrderStatus AS s ON ( p.ProductionOrderID = s.ProductionOrderStatusID ) 
    LEFT JOIN NotGood AS n ON ( p.ProductionOrderID = n.NGID ) 
    LEFT JOIN BatchOrder AS b ON ( p.ProductionOrderID = b.ProductionOrderID)//Changed this equation 
    LEFT JOIN Brand AS bd ON ( p.ProductionOrderID = bd.BrandID ) 
    LEFT JOIN CustomerOrder AS co ON ( p.ProductionOrderID = co.COID ) 
    LEFT JOIN Customer AS c ON ( p.ProductionOrderID = c.CustomerID ) 
    LEFT JOIN CustomerOrderStatus AS cos ON ( p.ProductionOrderID = cos.COStatusID )
        WHERE p.ProductionOrderID='$OrderId'")
    
        2
  •  1
  •   Shahriar63    8 年前

    在while循环内键入

    echo "<pre>";
    print_r($row);
    echo "</pre>";
    

    选择查询 .