gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:32:06 25 4
gpt4 key购买 nike

我似乎无法确定为什么我的 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();
?>

最佳答案

我想通了为什么会发生它是由于连接类型 INNER this将帮助您理解。

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

如果您想在 HTML 中的一列中显示所有批处理详细信息,那么建议使用 whileforeach 并且您不需要另一个 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'")

关于php foreach循环只返回数组中第一次迭代的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46313392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com