![](/img/trans.png)
[英]Display data from multiple tables and group them by a value (PHP/MySQL)
[英]How to join multiple tables in MySQL, then use PHP to display them as a group of tables for each new parent ID
首先,这是我的ERD提供必要的背景信息:
https://i.imgur.com/idVR5ev.png
其次,这是我希望我的代码实现的预期最终结果:
https://i.imgur.com/4h7l49p.png
确认:我希望能够创建任意数量的新生产订单(父订单),这些订单可以具有零个,一个或多个批处理订单(子代),而这些批处理订单本身由零个,一个或多个客户订单(子代的孩子)组成)。
我的主要问题是知道如何为每个ProductionOrderID复制3个表(ProductionOrder,BatchOrder和CustomerOrder),如第二个屏幕截图所示。
到目前为止,我的进度:
连接-db.php中
<?php
$server = "";
$user = "";
$pass = "";
$db = "";
$mysqli = new mysqli($server, $user, $pass, $db);
mysqli_report(MYSQLI_REPORT_ERROR);
?>
这是我的知识停止的地方,无法找到可以满足需求的现有示例,但是到目前为止我已经尝试过:
的index.php
<?php
include('connect-db.php');
if ($result = $mysqli->query(
"SELECT * FROM ProductionOrder p
INNER JOIN ProductionOrderStatus s
ON p.ProductionOrderID = s.ProductionOrderStatusID
INNER JOIN NotGood n
ON p.ProductionOrderID = n.NGID
INNER JOIN BatchOrder b
ON p.ProductionOrderID = b.BatchID"
))
{
if ($result->num_rows > 0)
{
echo "<table class='table table-striped'>";
echo "<tr> <th>PO ID</th> <th>PO #</th> <th>Order Quantity</th> <th>Balance Left</th> <th>Production Date</th> <th>PO Status</th> </tr>";
while ($row = $result->fetch_object())
{
echo "<tr>";
echo "<td>" . $row->ProductionOrderID . "</td>";
echo "<td>" . $row->PONum . "</td>";
echo "<td>" . $row->OrderQTY . "</td>";
echo "<td>" . $row->BalLeftNum . "</td>";
echo "<td>" . $row->ProductionDate . "</td>";
echo "<td>" . $row->ProductionOrderStatus . "</td>";
echo "</tr>";
echo "</table>";
//Display Batch Orders for contained Production Order
echo "<table class='table table-striped'>";
echo "<tr> <th>Batch ID</th> <th>Batch Quantity</th> <th>Available Date</th> <th>Balance Left</th> <th>Production Order</th> </tr>";
echo "<tr>";
echo "<td>" . $row->BatchID . "</td>";
echo "<td>" . $row->BatchQTY . "</td>";
echo "<td>" . $row->AvailDate . "</td>";
echo "<td>" . $row->RemainBal . "</td>";
echo "<td>" . $row->ProductionOrderID . "</td>";
echo "</tr>";
echo "</table>";
//Display Customer Orders
echo "<table class='table table-striped'>";
echo "<tr> <th>Customer Order ID</th> <th>Invoice Quantity</th> <th>Invoice Number</th> <th>Shipment Date</th> <th>Batch Order</th> </tr>";
echo "<tr>";
echo "<td>" . $row->COID . "</td>";
echo "<td>" . $row->InvoiceQTY . "</td>";
echo "<td>" . $row->InvoiceNum . "</td>";
echo "<td>" . $row->ShipDate . "</td>";
echo "<td>" . $row->BatchID . "</td>";
echo "</tr>";
echo "</table>";
}
}
else
{
echo "No results to display!";
}
}
else
{
echo "Error: " . $mysqli->error;
}
$mysqli->close();
?>
我完全知道这段代码只能显示一次结果,但是我想展示一些东西而不是什么,因为我希望了解需求,而不仅仅是有人写下答案而不问任何问题...
我不知道如何正确地联接我需要的表并将它们分组。 我尝试了GROUP BY,INNER JOIN,并尝试使用PHP创建树,但无法通过任何方法获得所需的结果。
在这里您可以做的是首先检索与生产订单ID相关的所有必需数据,例如这样
$result = [
0:{
"poid":1,
"po":2,
"order_quantity":3,
"batch_id": 2,
"customer_id": 4,
and all the required data for production order one
},
1:{
"poid":1,
"po":2,
"order_quantity":3,
"batch_id": 2,
"customer_id": 4,
and all the required data for production order two
}
]
这个数组结构可能不正确,但是可以在foreach循环中使用类似的东西。 现在您可以在foreach中循环数组并打印表(即三个表)。 例如
foreach($result as $r){
//PO table
<table>
<thead>
<tr>
<td>PO ID</td>
<td>PO#</td>
<td>Balance left</td>
//etc.
<tr>
</thead>
<tbody>
<tr>
<td><?php echo $r->poid; ?>
<td><?php echo $r->po; ?>
<td><?php echo $r->po_quantity; ?>
//etc.
</tr>
</tbody>
</table>
//Batch table
<table>
<thead>
<tr>
<td>Batch Id</td>
<td>Batch Quantity</td>
<td>Abailable date</td>
//etc.
<tr>
</thead>
<tbody>
<tr>
<td><?php echo $r->batchid; ?>
<td><?php echo $r->batchquantity; ?>
<td><?php echo $r->date; ?>
//etc.
</tr>
</tbody>
</table>
//and same for customer table
}
希望这给你一些想法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.