2017-09-20 75 views
4

我似乎无法确定为什么我的foreach循环能够循环创建所有5个ProductionOrderID,但只返回第一个ID的数据。php foreach循环只返回数组中第一次迭代的值

这是我的理解是,阵列正确循环,你可以看到当前结果这里: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 新的,更新的代码(感谢沙) :

<?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(); 
?> 
+0

你想循环5每页或5记录每次'SQL'运行?或者你只是想在你的表中显示记录? – Sand

+0

我想循环每条记录(ProductionOrder);这是目前的5,但可能是5或500. –

+0

好,你能告诉我,'$ POIds'做什么? – Sand

回答

3

我想通了它为什么发生这是由于连接类型INNERthis会帮助你理解。

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

如果你想显示在你的HTML在一列中所有批次的详细信息,然后建议whileforeach,你不需要再SQL您已经选择的行。 EX:$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'") 
+0

非常感谢你!对于视觉示例,这绝对有助于澄清不同的类型。再次感谢您花时间理解和帮助。 –

+0

我确实注意到它没有显示每个生产订单超过1个批次或客户订单。你有什么建议如何循环浏览每一个> –

+0

对不起,当你评论时,我已经离线了。没问题,你的问题也让我有机会刷上我的'SQL'。关于你的第二条评论是在纠正了SQL之后呢?你的意思是你的系统只有在同一个_Customer Order_下有更多的数据时才显示一组数据?你也使用相同的'SQL'或这是不同的? – Sand

1

while循环

echo "<pre>"; 
print_r($row); 
echo "</pre>"; 

内部类型,这样你可以看到你的数据的行为。我认为从您的选择查询提出的主要问题。