在MySQL数据库中,我有两个表 Abc 和 Pqr . 在 Abc 表中有一个唯一的ID,该ID在 Pqr 表中用作外键 .
我想将父元素显示为 Abc 表数据,将子行显示为 Pqr 表数据,相对于 Abc 唯一ID .
这是我的代码:
$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 回答
为什么不在select语句中使用JOIN或Subquery?我认为这会对你有所帮助,因为你在表上使用了关系模式 . 例:
$sqlGetParents =
在HTML表格中,我建议使用FOREACH而不是WHILE .
}?>
您可以根据结果更改$行号,或使用文本轻松了解应在表上显示哪些列 . (即$ row ['project_id'])