php if foreach个数记录,php foreach循环只返回数组中第一次迭代的值

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值