使用php循环从

时间:2015-12-02 15:33:57

标签: php mysql html5 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;
}

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

2 个答案:

答案 0 :(得分:1)

我不明白为什么你要调用这么多领带功能,以及为什么要做这么多循环。让我试试你的代码:

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

答案 1 :(得分:0)

由于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>

这也引入了short tag语法(<?=)来替换对echo的调用。如果您的PHP版本是&lt; this thread,请阅读5.4。这也引入了control structure alternate syntax,以便更好地使用HTML。上述方法将从DB中选择数据的关注与HTML的呈现分开。这样可以更轻松地进行调试,以及将来适应和模块化应用程序。