php - 带有固定 header 的可滚动 echo 数据库表

标签 php html css mysql

我正在尝试制作一个带有固定标题的可滚动表格。如果将数据输入到 HTML 中,这很有效,但是当我尝试将数据库的内容回显到表中时,滚动条困惑并且我的标题不再固定。如图所示,滚动条位于每个表条目上。我试过像某些人那样用 .div 包裹表格主体,但它不起作用。

enter image description here

 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>

关于php - 带有固定 header 的可滚动 echo 数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52039938/

相关文章:

javascript - 使用 AJAX 在 PHP 中空白 $_POST var

php - Codeigniter 方法将所有发布的值保存在二维数组中

html - 命令按钮操作方法未执行

javascript - Bootstrap - header 中的标识但不是 li-segment 的一部分

html - Flexbox 元素不会在浏览器窗口调整大小时重新排序,但会在响应模式下重新排序

javascript - 从无序列表中获取点击值

css - 无法让背景图像显示 - 不是路径问题

php - 有没有办法可以使用表单更改 PHP 页面从 MYSQL 数据库中选择的位置?

php - Laravel:获取 User::create 的 ID 并使用该 ID 插入新行

javascript - 从解析的 JSON 对象中获取元素