我的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>"
?>
希望这有帮助
表格标题将显示在部门名称下方
<?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>"
?>
您可以使用 mysql 的内置函数,该函数mysql_field_name
使用此功能,您将能够获取字段名称。尽管它在 php 5.4.0 或更高版本中已弃用,但您可以检查http://php.net/manual/en/mysqli-result.fetch-field-direct.php
从数据库获取数据时,仅从服务器获取实际数据。
因此,如果您需要输入表标题,您可以在 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>"
?>