php - 如何使用 PHP Mysql 创建服务器机架表
问题描述
我正在用 PHP 和 MySQL 创建一个机架服务器。首先,我需要使用行跨度创建动态表,然后我将尝试使用 Ajax 拖放。
我浏览了帖子并尝试了所有可用的解决方案,但无法使行跨度和 ID 对齐。
我非常感谢任何建议、链接或解决方案来实现结果。
MySQL
id name
1
2 test
3 test
4
5 server1
6 server1
7 server1
8
9 RHM
10
$sql = "select * from nasser ORDER BY id DESC, name DESC";
$query = mysqli_query($dbconnect,$sql);
while ($row = mysqli_fetch_array($query))
{
$result[$row['name']][] = $row['id'];
}
?>
<!DOCTYPE html>
<html>
<body>
<table id="invoices" border="1">
<thead>
<th>name</th>
<th>ID</th>
</thead>
<tbody>
<?php
foreach($result as $name => $id) {
echo '<tr>';
echo '<td rowspan='. count($id) . '>' . $name . '</td>';
$count = 0;
foreach ($id as $id) {
if ($count != 0) {
echo '<tr>';
}
echo "<td>$id</td>";
echo "</tr>";
$count++;
}
}
?>
</tbody>
</table>
</body>
</html>
最终结果应如下所示:
我无法让行对齐,它目前看起来像这样:
解决方案
最后,我通过参考这篇文章 Dynamic rowspan while fetching records from database 和 Dinesh Patra 的答案来实现它。
在发布问题之前,我尝试了他的代码,但是查看时表格被破坏了。
查看结果源后,我注意到如果所有没有数据的行都有一个行跨度,这会导致表中断。
因此,如果我的数据库表在所有行中都有数据,那么他的代码运行良好,但我需要一些行没有数据。
我修改了他的代码以包括一个简单的检查行值是否为空。
在完整代码下方。
<html>
<head>
<style>
table tr td, table tr th{
border: black 1px solid;
padding: 5px;
}
</style>
</head>
<body>
<?php
define("HOST", "localhost");
define("USER", "root");
define("PASSWORD", "");
define("DATABASE", "rack");
$dbconnect = new mysqli(HOST, USER, PASSWORD, DATABASE);
$sql = "select * from test ORDER BY id DESC, name DESC";
$result = mysqli_query($dbconnect,$sql);
$arr = array();
# Intialize the array, which will
# store the fetched data.
$id = array();
$name = array();
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
# data saving and rowspan calculation #
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
# Loop over all the fetched data, and save the
# data.
while($row = mysqli_fetch_assoc($result)) {
array_push($id, $row['id']);
array_push($name, $row['name']);
if (!isset($arr[$row['name']])) {
$arr[$row['name']]['rowspan'] = 0;
}
$arr[$row['name']]['printed'] = 'no';
$arr[$row['name']]['rowspan'] += 1;
}
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# DATA PRINTING #
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
echo "<table cellspacing='0' cellpadding='0'>
<tr>
<th>id</th>
<th>name</th>
<th>id</th>
</tr>";
for($i=0; $i < sizeof($id); $i++) {
$valName = $name[$i];
echo "<tr>";
# If this row is not printed then print.
# and make the printed value to "yes", so that
# next time it will not printed.
echo "<td>".$id[$i]."</td>";
if(empty($valName)) //if name is empty, print blank td
{
echo "<td></td>";
}
else
{
if ($arr[$valName]['printed'] == 'no')
{
echo "<td rowspan='".$arr[$valName]['rowspan']."'>".$valName."</td>";
$arr[$valName]['printed'] = 'yes';
}
}
echo "<td>".$id[$i]."</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
推荐阅读
- vue.js - 使用 Dropzone 将图像转换为 base64
- r - R:pivot_wider 问题:“值不是唯一标识的”
- c++ - 如何通过附加功能运行
- unix - 我想使用 sshapss 命令从 windows 机器获取文件到 unix 机器
- javascript - Lodash 在同一对象的关键部分下移动对象
- python - 当我们有几个组时,基于控件的样本标准化
- java - Hibernate 一对一映射,外键为 NULL
- c++ - easywsclient websocket 连接问题
- postgresql - 更新行 PostgreSQL
- excel - 使用带有变量名的范围的运行时错误