php - 如何在 MySQL 中连接多个表,然后使用 PHP 将它们显示为每个新父 ID 的一组表

标签 php mysql

首先,这是我的ERD,以提供必要的背景:

https://i.imgur.com/idVR5ev.png

其次,这是我希望我的代码实现的预期最终结果:

https://i.imgur.com/4h7l49p.png

确认:我希望能够创建任意数量的新生产订单(父级),其中可以有零个、一个或多个批量订单(子级),其中批量订单本身由零个、一个或多个客户订单组成( child 的 child )。

我的主要问题是知道如何为每个 ProductionOrderID 复制 3 个表(ProductionOrder、BatchOrder 和 CustomerOrder),如第二个屏幕截图所示。

<小时/>

到目前为止我的进展:

connect-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
 }

希望这能给你一些想法

关于php - 如何在 MySQL 中连接多个表,然后使用 PHP 将它们显示为每个新父 ID 的一组表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46291111/

相关文章:

php - 如何在 PHP 中继承单例?

php - Laravel中的错误处理

mysql - Laravel Eager Loading 两端都有一些 where 条件

php - 为什么这个 MySQL 查询在添加更多条件时会变慢?

php - 从类内的函数传递变量并在类外回显

php - 如何将 PHP 数组中的最后 n 项作为另一个数组获取?

mysql - 如何找到mysql中某列中缺失的月份?

php - 将 id 重新排序为连续的

mysql dump - 排除一些表数据

mysql - 如何在 mysql 数据库中存储具有 true/false 值的 xml 文件