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

时间:2017-09-20 04:37:40

标签: php mysql arrays foreach inner-join

我似乎无法确定为什么我的foreach循环能够为所有5个ProductionOrderID创建循环,但只返回第一个ID的数据。

我的理解是阵列正确循环,因为您可以在此处看到当前结果https://i.imgur.com/JWD3nis.png但奇怪的是ID:2没有生成表格并且ID 5创建了2个表,根据刚刚链接的imgur屏幕截图全部为空白。

我已经检查了我的样本数据,每张表有5条唯一记录,没有重复或我可以找到的问题。

编辑:1 我忘了提到所需的结果,以澄清我希望循环工作的方式。请看这个截图:https://i.imgur.com/4h7l49p.png(干杯沙)。

编辑:2 这是SQL的导出:https://pastebin.com/MG2gtASu 这是我的 ERD :如果有帮助: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 个答案:

答案 0 :(得分:3)

我弄清楚为什么由于加入类型INNER this而发生这种情况会帮助您理解。

发现问题,为什么它只显示1批数据。这是因为您等于p.ProductionOrderID = b.BatchID所以会发生什么情况,查询会将您的生产ID与批次ID相匹配,而您的批次ID是唯一的,因此没有重复项会导致显示单个数据从该匹配行记录。您真正想要做的是匹配批处理表中的生产ID,因为这是生产表和批处理表之间的关系。现在,当你运行它时,它将绘制表直到批处理结束。

如果您想在HTML中的一列中显示所有批次详细信息,请建议whileforeach并且您不需要另一个SQL您已经拥有行选择。 EX:$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'")

答案 1 :(得分:1)

在while循环中输入

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

因此,您可以看到数据的行为。我认为主要问题来自 选择查询