2015-12-02 124 views
1

需要以表格形式显示mysql表格中检索到的数据,但没有得到我计划的正确显示。这是我希望它看起来像 Target display以php表格形式从mysql数据库以php的形式显示数据

但是,这是我根据我的代码是什么得到 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; 
} 

Is there一个更好的方式来编写循环并以正确的方式显示我的数据?我查了类似的线程,但仍然无法掌握。

+1

为什么你调用find_all_employee()函数3次? – Stavros

回答

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

您可能还需要编辑上面的代码以包含'while'循环的右括号。 – Todd

+0

仍在编辑,以使其更具可读性,谢谢 – Standej

+0

此代码片段也可以通过在任何HTML打印之前执行数据库查询来改进。只需将结果存储在数组中,然后在HTML表格内使用'foreach'循环访问数组。这将更好地反映PHP脚本和HTML代码的不同责任,并且还允许更容易的错误处理(如果发生数据库错误或没有找到记录等)。 – Todd

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> 

这还引入了更换呼叫echoshort tag语法(<?=)。如果您的PHP版本是< 5.4,请阅读this thread。这也引入了control structure alternate syntax,为了更好的阅读HTML。上述方法分离了从HTML表示中选择数据的关注点。这样可以更轻松地进行调试,以及将来的适应和模块化应用程序。

+0

这是非常有用的,我将使用它正在工作的另一个页面,看看它是如何去。 – Mena