dongmei5168 2018-12-10 11:20
浏览 65

使用mysql的子行的数据表

In MySQL Database, I have two tables Abc and Pqr. In Abc table there's a unique ID, that ID is used in Pqr table as foreign key.

I want to show the parent element as Abc table data and child rows as Pqr table data with respect to Abc unique ID.

Here is my code:

$sqlGetParents="SELECT * from projectrera order by project_id";
$resultGetParents = $conn->query($sqlGetParents);   
?>
<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th></th>
            <th>Project Name</th>
            <th>Builder Id</th>
            <th>Location Id</th>
            <th>Phase</th>
            <th>Status</th>
        </tr>
    </thead>   
      <?php
      while ($row = mysqli_fetch_array($resultGetParents))  { 
       echo " <tr>
            <td class='details-control'></td>
            <td>".$row[1]."</td>
            <td>".$row[2]."</td>
            <td>".$row[3]."</td>
            <td>".$row[8]."</td>
            <td>".$row[15]."</td>
        </tr>";
     }   ?>   
</table>  
<div id="test">
    <table id='example1'>
<?php 
$sqlGetCatWithParent1="SELECT * from info";
 $resultGetCatWithParent1 = $conn->query($sqlGetCatWithParent1);
while ($row3 = mysqli_fetch_array($resultGetCatWithParent1)) {
 echo " <tr>
            <td></td>
            <td>".$row3[1]."</td>
            <td>".$row3[2]."</td>
            <td>".$row3[3]."</td>
        </tr>";
}
?>
  • 写回答

1条回答 默认 最新

  • doujin4031 2018-12-10 11:39
    关注

    Why don't you use the JOIN or Subquery on your select statement? I think that would help you since you're using a relational schema on your tables. Example:

    $sqlGetParents =

    SELECT abc.*, pqr.* from projectrera abc
      LEFT JOIN info pqr on pqr.project_id = abc.project_id
    order by project_id
    

    In your HTML table, I suggest to use FOREACH instead of WHILE.

    <?php foreach ($row->result() in $resultGetParents)  { 
      echo "<tr>
            <td class='details-control'></td>
            <td>".<?php echo $row[1]."</td>
            <td>".$row[1]."</td>
            <td>".$row[3]."</td>
            <td>".$row[8]."</td>
            <td>".$row[15]."</td>
        </tr>";
      echo "<tr>
            <td></td>
            <td>".$row[1]."</td>
            <td>".$row[2]."</td>
            <td>".$row[3]."</td>
        </tr>";
    

    } ?>

    You can change the $row number based on the results or use text to easily know which columns should be displayed on your table. (i.e $row['project_id'])

    评论

报告相同问题?

悬赏问题

  • ¥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,出参布尔值