我似乎无法确定为什么我的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();
?>
解决方法:
我弄清楚为什么它会发生这是因为INNER this的连接类型会帮助你理解.
发现了为什么它只显示1批数据的问题.这是因为你等于p.ProductionorderID = b.BatchID所以会发生什么是查询看起来将您的生产ID与批次ID相匹配,并且您的批次ID是唯一的,因此没有重复项导致从该匹配行显示单个数据记录.您真正想要做的是匹配批处理表中的生产ID,因为这是生产表和批处理表之间的关系.现在,当你运行它时,它将绘制表直到批处理结束.
如果要在HTML中的一列中显示所有批处理详细信息,则建议使用或预处理,并且不需要另一个sql,您已经选择了行. EX:$行[ “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'")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。