以字段作为主标题显示mysql表

时间:2017-02-08 08:57:15

标签: php html mysql

我有一个我在php页面上显示的mysql数据表。它有效但我希望它们归入它们所属的部门。还要删除所有其他字段标题,例如姓名,电话电子邮件。

我尝试使用SORT BY但没有发生任何事情。

例如。这就是我希望它看起来像

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

目前的情况如下:

ID    Name       Phone       Email               Department
1     Bob        3234234     bob@asasdas.com     Vehicle Department
2     Hanna      3434323     hanna@asasdas.com   Workshop Department

我目前的代码:

<?php

    $db_host = 'localhost';
    $db_user = 'root';
    $db_pwd = '*****';

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

    if (!mysql_connect($db_host, $db_user, $db_pwd))
        die("Can't connect to database");

    if (!mysql_select_db($database))
        die("Can't select database");

    // sending query
    $result = mysql_query("SELECT * FROM {$table}");
    if (!$result) {
        die("Query to show fields from table failed");
    }

    $fields_num = mysql_num_fields($result);

    echo "<table border='1'><tr>";
    // printing table headers
    for($i=0; $i<$fields_num; $i++)
    {
        $field = mysql_fetch_field($result);
        echo "<td>{$field->name}</td>";
    }
    echo "</tr>\n";
    // printing table rows
    $temp = "";

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

        if ($row['department'] != $temp){
        echo "<td colspan=\"3\">" . $row['department'] . "</td></tr>\n<tr>";
        $temp = $row['department'];
    }

    echo "<td>" . $row['name'] . "</td><td>" . $row['phone'] . "</td><td>" . $row['email'] . "</td>";

    echo "</tr>\n";
    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

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

2 个答案:

答案 0 :(得分:1)

如果您使用

"SELECT name, phone, email, department FROM {$table} ORDER BY department"

作为您的查询,它只返回您想要的列,按department列排序 然后你可以根据他们的部门安排在你的桌子上。

请注意,您可能需要调整列名称。

试试这段代码:

<?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, phone, email, department FROM {$table} ORDER BY department");
    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=\"3\" align=\"center\">" . $row['department'] . "</td></tr>\n<tr>";
            $temp = $row['department'];
        }

        echo "<td>" . $row['name'] . "</td><td>" . $row['phone'] . "</td><td>" . $row['email'] . "</td>";

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

答案 1 :(得分:0)

首先,您必须处理数据以排列您想要的结构。

<?php

$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '*****';

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

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<table border='1'><tr>";
// printing table headers
for ($i = 0; $i < $fields_num; $i++) {
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";

//variable to store rearranged results;
$resultsModified = [];

// printing table rows
while ($row = mysql_fetch_row($result)) {
    if (!isset($resultsModified[$result['department']]))
        $resultsModified[$result['department']] = [];

    $resultsModified[$result['department']][] = $row;
}

foreach ($resultsModified as $daptName => $results) {
    echo "<caption> $daptName </caption>";
    foreach ($results as $row) {
        echo "<tr>";

// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
        foreach ($row as $cell)
            echo "<td>$cell</td>";

        echo "</tr>\n";
    }
}

mysql_free_result($result);

但是我没有测试过,但这应该可行。