2017-09-20 76 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>"; 

內部類型,這樣你可以看到你的數據的行爲。我認爲從您的選擇查詢提出的主要問題。