【问题标题】:Codeigniter: How to avoid displaying duplicated values or overwrite a duplicated value in PHP foreach loop in a table?Codeigniter:如何避免在表格中的 PHP foreach 循环中显示重复值或覆盖重复值?
【发布时间】:2016-01-06 01:06:33
【问题描述】:

我目前正在研究 codeigniter。我想显示一个未重复的值或将 MySQL 数据库中的重复值覆盖到 PHP foreach 循环的数据表中。

这是我在数据库中的 2 个表的图片:

Table "employees" & table "times"

这是我的 2 个表的关系:

Relation of table "employees" & table "times"

这是我的控制器(home.php):

public function goPresent() { // attendance present page
    $this->load->model('Model_attendance');
    $query = $this->Model_attendance->getEmployees();
    $data['EMPLOYEES'] = null;
    if ($query) {
        $data['EMPLOYEES'] = $query;
    }
    $this->load->view('imports/header');
    $this->load->view('imports/menu');
    $this->load->view('attend', $data);
}

这是我的模型(model_attendance.php):

class Model_attendance extends CI_Model {

    public $userID;
    public $username;
    public $password;
    public $totime;
    public $absence_type;
    public $date;
    public $hours;

    public function getEmployees() {
        $this->db->select("*");
        $this->db->from('times');
        $this->db->join('employees', 'employees.empnum = times.userID');
        $query = $this->db->get();
        return $query->result();
    }
}

这是我的观点(attend.php):

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

【问题讨论】:

  • 感谢凯文的编辑。
  • 你的意思是如果有重复的用户ID,它只显示1?对不起,我的英语不好
  • 尝试:$this-&gt;db-&gt;join('employees', 'employees.empnum = times.userID','left'); 您还可以将表格编辑为您不想复制的列的唯一值
  • 如果有重复的用户 ID,我希望它避免然后它将显示到 PHP foreach 循环表中。
  • 它没有用。请给我一个完整的mvc代码。谢谢。

标签: php mysql codeigniter


【解决方案1】:

您想要唯一值的依据,只需在查询中添加这一行......

$this->db->group_by("column name which needs to be unique");

【讨论】:

    【解决方案2】:
     <?php
                                    $customers =   $this->db->group_by('customer_id')->get_where('registered_table' , array(
                                        'chat_group_id' => $group_id , 'year' => $reg_year
                                    ))->result_array();
                                    foreach($customers as $row):?>.....
    

    上面的示例代码将找到重复的 customer_id 并避免按照您的意图打印出重复的值。这里我使用了 group_by('customer-id')

    【讨论】:

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