首页 > 解决方案 > 如何使用 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>

最终结果应如下所示:

期望的结果

我无法让行对齐,它目前看起来像这样:

当前结果

标签: phphtmlmysqlhtml-table

解决方案


最后,我通过参考这篇文章 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>

桌子图片


推荐阅读