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).

编辑: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'")

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...