2015-12-31 33 views
-2

我目前正在研究codeigniter。我想显示一个不重复的值,或者将mysql数据库中的重复值覆盖到php foreach循环的数据表中。如何避免显示重复值或覆盖重复值在PHP foreach循环表中?

下面是表代码:

<table class="table table-striped responsive-utilities jambo_table" id="myTable"> 
            <thead> 
             <tr class="headings"> 
              <th>Employee No.</th> 
              <th>Username</th> 
              <th>Name</th> 
              <th>Status</th> 
             </tr> 
            </thead> 
            <tbody> 
             <?php 
             foreach($EMPLOYEES as $employee){?> 
             <tr> 
              <td><?php echo $employee->empnum; ?></td> 
              <td><?php echo $employee->username; ?></td> 
              <td><?php echo $employee->name; ?> <?php echo $employee->lastname; ?></td> 
              <td><?php 
              if ($employee->hasClockOut==1){ 
               echo '<a class="label label-danger">Inactive</a>'; 
              }else { 
               echo '<a class="label label-success">Active</a>'; 
              } 
              ?></td> 
             </tr> 
             <?php } ?> 
            </tbody> 
           </table> 
+0

你为什么不只能选择唯一值,在查询 –

+0

它不正常工作。 – Jorge

+1

对您的数据和预期结果的解释可能有助于澄清您的需求并为您提供更准确的答案。 “不起作用”真的很少解释。 – Niagaradad

回答

0

为什么不直接使用 -

$员工= array_unique($职员);

+0

它不起作用。 :( – Jorge

+0

那么,你需要发布$ EMPLOYEES的内容,并且稍微回复一下你想做的事情,因为它不清楚。 – Andrew

0

试试这个

<table class="table table-striped responsive-utilities jambo_table" id="myTable"> 
    <thead> 
     <tr class="headings"> 
      <th>Employee No.</th> 
      <th>Username</th> 
      <th>Name</th> 
      <th>Status</th> 
     </tr> 
    </thead> 
    <tbody> 
     <?php $emp=''; 
     foreach($EMPLOYEES as $employee){ 
     if($emp!=$employee->username){ # if username contain duplicate values 
     ?> 
     <tr> 
      <td><?php echo $employee->empnum; ?></td> 
      <td><?php echo $employee->username; ?></td> 
      <td><?php echo $employee->name; ?> <?php echo $employee->lastname; ?></td> 
      <td><?php 
      if ($employee->hasClockOut==1){ 
       echo '<a class="label label-danger">Inactive</a>'; 
      }else { 
       echo '<a class="label label-success">Active</a>'; 
      } 
      ?></td> 
     </tr> 
     <?php }$emp=$employee->username;} ?> 
    </tbody> 
</table> 
+0

它没有工作。:( – Jorge