gpt4 book ai didi

javascript - 使用 mysql 的带有子行的数据表

转载 作者:行者123 更新时间:2023-11-29 16:34:14 26 4
gpt4 key购买 nike

在 MySQL 数据库中,我有两个表 AbcPqr。在Abc表中有一个唯一的ID,该ID在Pqr表中用作外键。

我想将父元素显示为 Abc 表数据,将子行显示为与 Abc 唯一 ID 相关的 Pqr 表数据。

这是我的代码:

$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>";
}
?>

最佳答案

为什么不在 select 语句中使用 JOIN 或子查询?我认为这会对您有所帮助,因为您在表上使用关系模式。示例:

$sqlGetParents =

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

在 HTML 表格中,我建议使用 FOREACH 而不是 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>";

} ?>

您可以根据结果更改 $row 编号,或使用文本轻松了解表格上应显示哪些列。 (即 $row['project_id'])

关于javascript - 使用 mysql 的带有子行的数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53704581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com