gpt4 book ai didi

php - 使用php循环以表格格式显示来自mysql数据库的数据

转载 作者:行者123 更新时间:2023-11-28 23:42:57 25 4
gpt4 key购买 nike

需要以表格格式显示从 mysql 表中检索到的数据,但没有得到我计划的正确显示。这就是我想要的样子 Target display

但这是基于我的代码得到的 Current display

这是html代码

<div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<tbody>

<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff first name, last name, department and action (edit and delete links)
?>
<tr>
<td>
<?php
echo $staff["first_name"];
}
?>
</td>

<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff last name
?>
<td>
<?php
echo $staff["last_name"];
}
?>
</td>

<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff department
?>
<td>
<?php
echo $staff["department"];
}
?>
</td>

<td>
<a href="edit_admin.php"><span class="glyphicon glyphicon-pencil"> Edit</span></a>
&nbsp
<a href=""><span class="glyphicon glyphicon-trash"> Delete</span></a>
</td>
</tr>

</tbody>
</table>
</div>

这是用于从我的员工表中查找所有员工列表的函数

function find_all_employee() {
global $connection;

$query = "select * from staff";
$staff_set = mysqli_query($connection, $query);
confirm_query($staff_set);
return $staff_set;
}

有没有更好的方法来编写循环并以正确的方式显示我的数据?我已经查找过类似的线程,但仍然无法理解。

最佳答案

我不明白你为什么要调用那么多 tie 函数,为什么要创建那么多循环。让我试试你的代码:

<div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff first name, last name, department and action (edit and delete links)
?>
<tr>
<td>
<?php echo $staff["first_name"]; ?>
</td>
<td>
<?php echo $staff["last_name"]; ?>
</td>
<td>
<?php echo $staff["department"]; ?>
</td>
<td>
<a href="edit_admin.php"><span class="glyphicon glyphicon-pencil"> Edit</span></a>
&nbsp
<a href=""><span class="glyphicon glyphicon-trash"> Delete</span></a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>

关于php - 使用php循环以表格格式显示来自mysql数据库的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34046653/

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