gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:23:06 25 4
gpt4 key购买 nike

我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com