2017-09-20 31 views
4

私のforeachループが生成されたProductionOrderIDの5つすべてに対してループすることができる理由を特定できないようですが、最初のIDのデータを返します。php foreachループは配列内の最初の反復の値を返すだけです

それは、配列が正しくループしていることをあなたがここに現在の結果を見ることができるように私の理解です:https://i.imgur.com/JWD3nis.pngが、どのような奇妙なのはIDです:あたりなど、すべてが空白、2が生成されている何のテーブルを持っていないし、ID 5が作成した2つのテーブルを持っていますimgurのスクリーンショットがリンクされました。

私はサンプルデータを2倍にしましたが、重複や問題が見つからないテーブルごとに5つのユニークなレコードがあります。

EDIT:1 どのようにループ機能を働かせたいかを明確にするために、欲しい結果を述べることを忘れました。このスクリーンショットを参照してください:https://i.imgur.com/4h7l49p.png(乾杯)。

EDIT:2 ここでは、SQLのエクスポートです:https://i.imgur.com/idVR5ev.png

EDIT:3 新しい、更新されたコード(おかげで砂)https://pastebin.com/MG2gtASu そして、ここには私のERDそれが役立つはずです:

<?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

「SQL」が実行されるたびに1ページまたは5レコードをループしますか?テーブルにレコードを表示したいだけですか? – Sand

+0

私はすべてのレコード(ProductionOrder)をループしたいです。現在は5ですが、5または500になる可能性があります。 –

+0

「$ POIds」はあなたに私に言うことができますか? – Sand

答えて

3

参加型のINNER 012によるだ起こって、なぜ私は考え出しましたはあなたの理解に役立ちます。

なぜ1バッチのデータが表示されたのかという問題が見つかりました。 p.ProductionOrderID = b.BatchIDと同じであるため、プロダクションIDをバッチIDと一致させるためのクエリルックとバッチIDが一意であるため、一致する行から単一のデータレコードを表示する重複はありません。本当にしたいのは、生産テーブルとバッチテーブルの関係なので、バッチテーブルの生産IDと一致させることです。これを実行すると、バッチの最後までテーブルが描画されます。

あなたのHTML内の1つの列内のすべてのバッチの詳細を表示したい場合は、whileまたはforeachを示唆すると、あなたがすでに行が選択されていSQL別のものを必要としません。 EX:$row["BatchQTY"]

これが私の解決です。 whileループ内の

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'をブラッシュアップする機会を与えてくれました。そして、あなたの2番目のコメントについては、これは 'SQL'を修正した後ですか?あなたのシステムは、同じ_顧客注文の下にもっと多くのデータがある場合にのみ1セットのデータを表示しますか?また、あなたは同じ 'SQL'を使用しているのですか?これは別ですか? – Sand

1

タイプ

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

だから、あなたはあなたのデータの動作を確認することができます。私は主な問題があなたのから選択したと思う。

関連する問題