doulin8374 2017-09-19 03:03
浏览 63
已采纳

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

Firstly, here is my ERD to provide the necessary background:

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

Secondly, here is the intended final result I wish my code to achieve:

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

To confirm: I want the ability to create any number of new ProductionOrders (parent) which can have Zero, One or Many Batch Orders (Child) which the Batch Orders themselves are made up of Zero, One or Many Customer Orders (Child of Child).

My main issue here is knowing how to duplicate the 3 tables (ProductionOrder, BatchOrder & CustomerOrder) for each ProductionOrderID as seen in my second screenshot.


My progress so far:

connect-db.php

<?php
$server = "";
$user = "";
$pass = "";
$db = "";
$mysqli = new mysqli($server, $user, $pass, $db);
mysqli_report(MYSQLI_REPORT_ERROR);
?>

This is where my knowledge stops and I am unable to find an existing example that I can work into my requirements but what I've tried so far:

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

I am fully aware this code is only capable of showing the results once but I wanted to show something vs nothing as I am hoping to understand the requirements and not just have someone write the answer no questions asked...

I do not know how to properly join the tables I need and group them. I have tried GROUP BY, INNER JOIN and tried to create a tree using PHP but have been unable to get the desired results via any method.

  • 写回答

1条回答 默认 最新

  • drsxobip501258 2017-09-19 05:38
    关注

    here what you can do is first retrieve all the required data with respect to production order id for example like this

    $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
    
       }
    ]
    

    this array structure might not be right but something like this which can be used in foreach loop. now you can loop array in foreach and print the table (i.e, three table). for example

     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
     }
    

    hope this gives you some idea

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值