我似乎无法确定为什么我的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).
编辑:3
新的更新代码(谢谢Sand):
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 "
Order: $OrderId
";print "
print "
PO ID PO # Order Quantity Balance Left Production Date Production Order Status Not Good ID ";print "
" . $row->ProductionOrderID . "";print "
" . $row->PONum . "";print "
" . $row->OrderQTY . "";print "
" . $row->BalLeftNum . "";print "
" . $row->ProductionDate . "";print "
" . $row->ProductionOrderStatusID . "";print "
" . $row->NGID . "";print "";
print "
";//BatchOrder
print "
print "
Batch ID Brand Name Batch Quantity Availability Date Remaining Balance Production Order ID ";print "
" . $row->BatchID . "";print "
" . $row->BrandID . "";print "
" . $row->BatchQTY . "";print "
" . $row->AvailDate . "";print "
" . $row->RemainBal . "";print "
" . $row->ProductionOrderID . "";print "";
print "
";//CustomerOrder
print "
print "
Customer ID Customer Name Invoice Quantity Invoice # Shipping Date Batch ID CO Status ";print "
" . $row->COID . "";print "
" . $row->CustomerID . "";print "
" . $row->InvoiceQTY . "";print "
" . $row->InvoiceNum . "";print "
" . $row->ShipDate . "";print "
" . $row->BatchID . "";print "
" . $row->COStatusID . "";print "";
print "
";}
}
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'")
标签:php,arrays,mysql,inner-join,foreach
来源: https://codeday.me/bug/20190622/1264110.html