【发布时间】: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->db->join('employees', 'employees.empnum = times.userID','left');您还可以将表格编辑为您不想复制的列的唯一值 -
如果有重复的用户 ID,我希望它避免然后它将显示到 PHP foreach 循环表中。
-
它没有用。请给我一个完整的mvc代码。谢谢。
标签: php mysql codeigniter