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

时间:2017-02-13 05:46:39

标签: 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数据库的名称,电话,电子邮件数据的字段标题。我的代码可以在下面找到:

<?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>"
?>

3 个答案:

答案 0 :(得分:1)

希望这有帮助

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

下方
 <?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>"
    ?>

答案 1 :(得分:0)

您可以使用mysql_field_name的mysql的内置函数使用此功能,您将能够获取字段名称。 虽然它在PHP 5.4.0或更高版本中已被弃用,但您可以查看 http://php.net/manual/en/mysqli-result.fetch-field-direct.php

答案 2 :(得分:0)

从数据库中获取数据时,只从服务器获取实际数据。

因此,如果您需要输入表头,可以在while循环之前插入HTML代码以插入标题。

<?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>";

/********  Add this! *********/
echo '<tr>
<th>Name</th>
<th>Phone</th>
<th>Email</th>
</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>"
?>