【发布时间】:2023-03-03 04:24:01
【问题描述】:
我似乎无法确定为什么我的 foreach 循环能够循环创建所有 5 个 ProductionOrderID,但只返回第一个 ID 的数据。
据我了解,数组循环正确,您可以在此处看到 当前结果:https://i.imgur.com/JWD3nis.png 但奇怪的是 ID:2 没有生成表,而 ID 5 有 2 个表已创建,根据刚刚链接的 imgur 屏幕截图全部为空白。
我已经仔细检查了我的示例数据,每个表都有 5 条唯一记录,我找不到任何重复或问题。
编辑:1 我忘了提到想要的结果来阐明我希望循环如何工作。请看这个截图:https://i.imgur.com/4h7l49p.png (Cheers Sand)。
编辑: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();
?>
【问题讨论】:
-
您想在
SQL每次运行时每页循环 5 条记录或 5 条记录?还是只想在表格中显示记录? -
我想循环每条记录(ProductionOrder);目前是 5,但可能是 5 或 500。
-
好的,你能告诉我
$POIds是做什么的吗? -
这是我尝试创建一个数组来存储所有
ProductionOrderID在foreach循环中被称为$OrderId的数组。结果当前如下所示:i.imgur.com/MIhfB0s.png -
是的,我试图弄清楚为什么你要使用
foreach如果你想显示数据表中的内容,只需使用while循环它就会循环直到没有更多要显示的数据。
标签: php mysql arrays foreach inner-join