php - 我的字段标题没有显示在我的 php 表中

标签 php html

我的 php 表显示在我的页面上,来自 mysql 数据库。它们按所在部门的顺序显示。

目前看起来是这样的:

                         Vehicle Department 
                Bob        3234234     bob@acas.com
                Hanna      3434323     Hanna@asas.com
                         Workshop Department
                Andrew     45454523    andrew@aasdasd.com

但如您所见,姓名、电话和电子邮件的字段标题未显示。我试图将它们与数据库结果一起回显,但它只会在表格中造成巨大的困惑。

我想要达到的最终结果:

                        Vehicle Department 
                Name        Phone        Email
                Bob        3234234     bob@acas.com
                Hanna      3434323     Hanna@asas.com
                        Workshop Department
                Name        Phone        Email
                Andrew     45454523    andrew@aasdasd.com

因此每个部门将显示来自mysql 数据库的name、phone、email 数据的字段标题。我的代码可以在下面找到:

<?php
    $db_host = 'localhost';
    $db_user = 'root';
    $db_pwd = '*****';

    $database = 'list';
    $table = 'users';

    $conn = mysqli_connect($db_host, $db_user, $db_pwd) or die("Connecting to database failed");

    mysqli_select_db($conn, $database) or die("Can't select database");

    // sending query
    $result = mysqli_query($conn, "SELECT name, email, extension, phone, department FROM {$table} ORDER BY department, name ASC");
    if (!$result) {
        die("Query to show fields from table failed");
    }

    echo "<table border='1'><tr>";

    // printing table rows
    $temp = "";

    while($row = mysqli_fetch_array($result)) {
        echo "<tr>";

        if ($row['department'] != $temp){
            echo "<td colspan='4' style='text-align: center; font-weight: bold'>{$row['department']}</td></tr>\n<tr>";
            $temp = $row['department'];
        }
    echo "<td>" . $row['name'] . "</td><td>" . $row['email'] . "</td><td>" . $row['extension'] . "</td><td>" . $row['phone'] . "</td>";

    echo "</tr>\n";
    }
    mysqli_free_result($result);
    echo "</table>"
?>

最佳答案

希望对你有帮助

表格标题将显示在部门名称下方

 <?php
        $db_host = 'localhost';
        $db_user = 'root';
        $db_pwd = '*****';

        $database = 'list';
        $table = 'users';

        $conn = mysqli_connect($db_host, $db_user, $db_pwd) or die("Connecting to database failed");

        mysqli_select_db($conn, $database) or die("Can't select database");

        // sending query
        $result = mysqli_query($conn, "SELECT name, email, extension, phone, department FROM {$table} ORDER BY department, name ASC");
        if (!$result) {
            die("Query to show fields from table failed");
        }

        echo "<table border='1'><tr>";

        // printing table rows
        $temp = "";

        while($row = mysqli_fetch_array($result)) {
            echo "<tr>";

            if ($row['department'] != $temp){
                echo "<td colspan='4' style='text-align: center; font-weight: bold'>{$row['department']}</td></tr>\n";
echo "<tr><th>Name</th>
<th>Phone</th>
<th>Email</th>
</tr>\n";
echo "<tr>";

                $temp = $row['department'];
            }
        echo "<td>" . $row['name'] . "</td><td>" . $row['email'] . "</td><td>" . $row['extension'] . "</td><td>" . $row['phone'] . "</td>";

        echo "</tr>\n";
        }
        mysqli_free_result($result);
        echo "</table>"
    ?>

关于php - 我的字段标题没有显示在我的 php 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42197394/

相关文章:

php - 如何为 Facebook 刷新/修改 Meta?

php - Lumen 文档中的 "function () use ($app) {"行意味着什么(可能在 Laravel 中有效)

javascript - Ajax调用JS+PHP不起作用

php - ajax 请求的响应在我的本地环境和我的实时服务器上是不同的

javascript - 删除和设置元素的文本

javascript - 如何使用正则表达式匹配任意字母组合?

javascript - `getElementById` 在 `<template>` 标签内找不到元素

php - Wordpress嵌入式视频问题

PHP更新大型数据库

html - 无法使用 css 和 prince 使分页符正常工作