【问题标题】:How to avoid displaying duplicated values or overwrite duplicated values in php foreach loop in table?如何避免在表中的php foreach循环中显示重复值或覆盖重复值?
【发布时间】:2015-12-31 02:07:28
【问题描述】:

我目前正在研究 codeigniter。我想显示一个没有重复的值或将mysql数据库中的重复值覆盖到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>

【问题讨论】:

  • 为什么不在查询中只选择唯一值
  • 它也不行。
  • 对您的数据和预期结果的解释可能有助于阐明您的需求并为您提供更准确的答案。 “不起作用”实际上解释得很少。
  • 你有脸书吗?这样我就可以给你发一张我的数据表的图片。
  • 使 empnum 唯一,以便表中不存在重复项。我认为您的表中有重复记录,这就是您在查询后得到重复行的原因。您使用 group_by 或分配唯一键empnum。

标签: php mysql codeigniter


【解决方案1】:

为什么不直接使用 -

$EMPLOYEES = array_unique($EMPLOYEES);

【讨论】:

  • 好吧,您需要发布 $EMPLOYEES 的内容以及更多关于您想要做什么的内容,因为它不清楚。
【解决方案2】:

试试这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    相关资源
    最近更新 更多