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

标签 php mysql html twitter-bootstrap-3

需要以表格格式显示从 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/

相关文章:

php - 尝试在 foreach 循环中获取 key 以使用 Blade 工作

php - 为什么 laravel 路由必须有优先顺序,否则找不到页面?

python - _mysql_exceptions.ProgrammingError : (1064, “你的 SQL 语法有错误;)

html - 将 header div 定位在屏幕顶部的中央

html - 为什么某些 div 中的内容显示在其框外?

javascript - 将 php 数组传递给 PHP 函数内的 javascript 数组

javascript - 包含 jQuery,如果还没有包含的话

php - 如何在一个PHP页面中只查询一次数据库中的数据,然后在多个PHP页面中使用?

MySQL - 使用选择查询更新

html - 无法在Phonegap应用中使用HTML5音频标签播放音频