gpt4 book ai didi

php - Codeigniter:如何避免在表中的 PHP foreach 循环中显示重复值或覆盖重复值?

转载 作者:行者123 更新时间:2023-11-29 11:47:43 24 4
gpt4 key购买 nike

我目前正在开发 codeigniter。我想显示一个不重复的值,或者将MySQL数据库中的重复值覆盖到PHP foreach循环的数据表中。

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

Table "employees" & table "times"

这是我的两个表的关系:

Relation of table "employees" & table "times"

这是我的 Controller (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>

最佳答案

您想要唯一值的基础,只需在查询中添加此行...

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

关于php - Codeigniter:如何避免在表中的 PHP foreach 循环中显示重复值或覆盖重复值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34624037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com