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

使用php循环以表格格式显示mysql数据库中的数据,php,mysql,html,twitter-bootstrap-3,Php,Mysql,Html,Twitter Bootstrap 3,需要以表格式显示从mysql表中检索到的数据,但没有得到我计划的正确显示。这就是我想要的样子 但这是我根据我的代码得到的 这是html代码 <div> <table class="table table-striped table-hover"> <thead> <tr> <th>First Name</th>

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

但这是我根据我的代码得到的

这是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>

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

我不明白为什么要调用这么多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>

名字
姓
部门
行动
 

因为OP要求在评论中使用不同的技巧:

<?php 
// Select all staff first name, last name, and department info
$staff_set     = find_all_employee();
$staff_results = array();
while ($staff = mysqli_fetch_assoc($staff_set)) {
    $staff_results[] = $staff;
}
?>

<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 if (!empty($staff_results)): ?>
            <?php foreach($staff_results as $staff_member): ?>
            <tr>
                <td>
                    <?= $staff_member["first_name"]; ?>
                </td>            
                <td>
                    <?= $staff_member["last_name"]; ?>
                </td>
                <td>
                    <?= $staff_member["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 endforeach; ?>
        <?php else: ?>
            <tr><td colspan="4">No Staff Members Found.</td></tr>
        <?php endif; ?>
        </tbody>
    </table>
</div>

名字
姓
部门
行动
 
没有找到工作人员。

这还介绍了语法(
为什么调用find_all_employee())函数3次?您可能还需要编辑上面的代码,以便在
while
循环中包含一个右大括号。在打印任何HTML之前执行DB查询也可以改进此代码段。只需将结果存储在数组中,然后在HTML表内使用
foreach
循环数组。This将更好地反映PHP脚本和HTML代码的不同职责,并允许更轻松地处理错误(在出现DB错误或未找到任何记录等情况下).@Standej修复了它,非常感谢您的帮助。@Todd我也很想学习如何做到这一点,请告诉我如何通过将结果存储在数组中来实现同样的效果。@Todd我同意我也更喜欢这样,只是我想对代码做更少的更改。另外,欢迎您!这非常有帮助,我将使用它另一个页面,我正在努力,看看它如何进行。
<?php 
// Select all staff first name, last name, and department info
$staff_set     = find_all_employee();
$staff_results = array();
while ($staff = mysqli_fetch_assoc($staff_set)) {
    $staff_results[] = $staff;
}
?>

<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 if (!empty($staff_results)): ?>
            <?php foreach($staff_results as $staff_member): ?>
            <tr>
                <td>
                    <?= $staff_member["first_name"]; ?>
                </td>            
                <td>
                    <?= $staff_member["last_name"]; ?>
                </td>
                <td>
                    <?= $staff_member["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 endforeach; ?>
        <?php else: ?>
            <tr><td colspan="4">No Staff Members Found.</td></tr>
        <?php endif; ?>
        </tbody>
    </table>
</div>