php - 带有 Fix header 的可滚动 Echo DB 表
问题描述
我正在尝试制作一个带有修复标题的可滚动表格。如果将数据输入到 HTML,这很有效,但是当我尝试将数据库的内容回显到表格时,滚动条会混乱,并且我的标题不再固定。如图所示,滚动条位于每个表格条目上。我已经尝试过 .div 像某些人那样将表格主体包裹起来,但它不起作用。
body{
background: lightblue;
}
#tble th {
text-align: left;
background-color: green;
color: white;
}
table{
display: block;
border: 1px solid;
margin-top: 10px;
width: 350px;
}
tbody{
display: block;
height: 50px;
overflow-y: scroll;
}
th {
width: 75px;
}
td {
width: 75px;
}
<html lang="en">
<body>
<table align="center" id="tble">
<thead>
<th style="text-align: center">ID</th>
<th style="text-align: center">Username</th>
<th style="text-align: center">Rights</th>
<th style="text-align: center">Age</th>
</thead>
<tbody>
<tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "account");
$result = mysqli_query($conn, "SELECT * FROM account.log WHERE rights IN ('Admin','User')", MYSQLI_USE_RESULT) or die(mysql_error());
while($row = mysqli_fetch_array( $result )) {
?>
<td style="text-align: center"><?php echo $row['id']; ?></td>
<td style="text-align: center"><?php echo $row['user']; ?></td>
<td style="text-align: center"><?php echo $row['rights']; ?></td>
<td style="text-align: center"><?php echo $row['age']; ?></td>
</tr>
</tbody>
<?php
}
?>
</body>
</table>
有什么我错过的吗?希望有人能指出我正确的方向。
解决方案
<!-- language: lang-html -->
<html lang="en">
<body>
<table align="center" id="tble">
<thead>
<th style="text-align: center">ID</th>
<th style="text-align: center">Username</th>
<th style="text-align: center">Rights</th>
<th style="text-align: center">Age</th>
</thead>
<tbody>
<tr>//move this from here
<?php
$conn = mysqli_connect("localhost", "root", "", "account");
$result = mysqli_query($conn, "SELECT * FROM account.log WHERE rights IN ('Admin','User')", MYSQLI_USE_RESULT) or die(mysql_error());
while($row = mysqli_fetch_array( $result )) {
?>
<tr> //to here
<td style="text-align: center"><?php echo $row['id']; ?></td>
<td style="text-align: center"><?php echo $row['user']; ?></td>
<td style="text-align: center"><?php echo $row['rights']; ?></td>
<td style="text-align: center"><?php echo $row['age']; ?></td>
</tr>
</tbody>
<?php
}
?>
</body>
</table>
推荐阅读
- lambda - 如何使用部署到 Netlify 的无服务器 lambda 函数隐藏条带密钥
- slurm - 如何在 SLURM 的分区中按节点组分配作业
- javascript - 使用 deleteRow 按 id 删除一行
- java - 使用 JNI 调用 C++ 库函数以及哪个进程执行该 C++ 库
- swift - 如何通过 ios 中的 cocoapods 使用 vendored 框架发送 dsym 文件
- c# - 如何添加引用 EF 核心中同一个表的多个外键
- typescript - 在运行时从数组值的 Observable 派生联合类型
- elixir - 无法运行 ecto.create,无法创建数据库:连接不可用并且请求已从队列中删除
- javascript - 当我使用变量作为参数时,数据不会出现
- reactjs - 将箭头功能更改为组件列表