my controller
public function admin() {
$data['employee'] = $this->attendance_model->get_attendance();
// get today's all employee attendance details
$data['attendance'] = $this->attendance_model->attendance();
// attendance page
$data['header'] = "Employee";
$data['sub_header'] = "Attendance employee";
$data['main_content'] = 'attendance/admin_list';
$this->load->view('employeelayout/main',$data);
}
my model
// get the employee details
public function get_attendance () {
return $this->db->get('employee')->result();
}
public function attendance() {
$today = date('Y-m-d'); // get today date
$this->db->where('A.date',$today)->from('attendance A')
->join('employee E','E.employee_id = A.employee_id','LEFT');
return $this->db->get()->result();
}
<thead>
<tr>
<th>#</th>
<th>Employee Name</th>
<th>Employee Number</th>
<th>Today</th>
<th>Mark Incoming</th>
<th class="hidden-phone">Status</th>
</tr>
</thead>
<tbody>
<?php $count = 1; ?>
<?php foreach($employee as $emp) { ?>
<tr class="gradeX">
<td><?php echo $count ++ ?></td>
<td><?php echo $emp->first_name.' '.$emp->last_name ?></td>
<td><?php echo $emp->employee_number ?></td>
<?php foreach($attendance as $atten) { ?>
<?php if($emp->employee_id == $atten->employee_id) { ?>
<td><?php echo $atten->date ?></td>
<td><?php echo $atten->mark_in_time ?></td>
<td><?php echo "present" ?></td>
<?php } else { ?>
<td></td>
<td></td>
<td><?php echo "absent" ?></td>
<?php } ?>
<?php } ?>
</tr>
<?php } ?>
</tbody>
我使用嵌套的foreach, 第一,我在第一个foreach中显示所有员工 第二,我想显示出席和缺席的员工出勤率 从出勤表我将employee_id与employee_id的员工表匹配 它显示所有现有员工,并正确显示缺席员工。任何帮助,请
我发布了图片,你可以看到,我显示所有员工,我正在检查employee-id = employee-id(表示存在于考勤表中)。如果存在则显示“存在”否则“缺席”。但它过度循环。
答案 0 :(得分:3)
SELECT e.*, CASE WHEN (a.attendance_id is null) THEN 'absent' ELSE 'present' END as presentinfo FROM `employee` e left join attendance a on e.employee_id=a.employee_id and a.date=date(now())
您的Html页面如下所示:
<tr class="gradeX">
<td><?php echo $emp->first_name.' '.$emp->last_name ?></td>
<td><?php echo $emp->employee_number ?></td>
<td><?php echo $emp->presentinfo ?></td>
</tr>
<?php } ?>
</tbody>
我的建议:使用上面的查询..它会显示您需要的结果。
答案 1 :(得分:0)
尝试以下
你的模特
public function get_attendance () {
$arrEmployee = $this->db->get('employee')->result();
$arrEmployeeById = array();
$arrEmployeeIds = array();
foreach($arrEmployee AS $objEmployee)
{
$objEmployee->arrAttendance = array();
$arrEmployeeById[$objEmployee->employee_id] = $objEmployee;
$arrEmployeeIds[] = $objEmployee->employee_id;
}
$this->db->where_in("A.employee_id",$arrEmployeeIds);
$arrAttendances = $this->attendance();
foreach($arrAttendances AS $objAttendence)
{
if (isset($arrEmployeeById[$objAttendence->employee_id])) $arrEmployeeById[$objAttendence->employee_id]->arrAttendance[] = $objAttendence;
}
return $arrEmployee;
//return $this->db->get('employee')->result();
}
public function attendance()
{
$today = date('Y-m-d'); // get today date
$this->db->where('A.date',$today)->from('attendance A')
//->join('employee E','E.employee_id = A.employee_id','LEFT');
return $this->db->get()->result();
}
你的控制器
public function admin() {
$data['employee'] = $this->attendance_model->get_attendance();
$data['header'] = "Employee";
$data['sub_header'] = "Attendance employee";
$data['main_content'] = 'attendance/admin_list';
$this->load->view('employeelayout/main',$data);
}
//你的观点
<thead>
<tr>
<th>#</th>
<th>Employee Name</th>
<th>Employee Number</th>
<th>Today</th>
<th>Mark Incoming</th>
<th class="hidden-phone">Status</th>
</tr>
</thead>
<tbody>
<?php $count = 1; ?>
<?php foreach($employee as $emp) { ?>
<tr class="gradeX">
<td><?php echo $count ++ ?></td>
<td><?php echo $emp->first_name.' '.$emp->last_name ?></td>
<td><?php echo $emp->employee_number ?></td>
<?php
if (count($emp->arrAttendance) > 0)
{
foreach($emp->arrAttendance as $atten) {
?>
<td><?php echo $atten->date ?></td>
<td><?php echo $atten->mark_in_time ?></td>
<td><?php echo "present" ?></td>
<?php
}
}
else
{
?>
<td></td>
<td></td>
<td><?php echo "absent" ?></td>
<?php } ?>
</tr>
<?php } ?>
</tbody>