答案 0 :(得分:0)
以下是实现解决方案的方法之一
注意:为了演示,我在控制器中包含了数据库查询。确保将其放在Models文件夹中。
$attendanceRecords = $this->db->get('attendance');
$attendaceDetails = array();
if($attendanceRecords->num_rows() > 0){
foreach($attendanceRecords as $key => $attendance){
if(!isset($attendaceDetails[$attendance['stud_id']])){
$attendaceDetails[$attendance['stud_id']][$attendance['a_date']] = $attendance['a_status'];
}else{
$attendaceDetails[$attendance['stud_id']][$attendance['a_date']] = $attendance['a_status'];
}
}
}
上面的代码将以下列格式创建循环
$attendaceDetails[
/* Key is student id */
[1] =>
/* Attendance respective to that date */
['2017-11-01'] => 'P',
['2017-11-02'] => 'P',
['2017-11-03'] => 'P'
[2] =>
['2017-11-01'] => 'P',
['2017-11-02'] => 'A',
['2017-11-03'] => 'A'
]
现在你可以进入工作日。例如
$workingDays = ['2017-11-01', '2017-11-02', '2017-11-04'];
/* This will get all the keys of the attendaceDetails ie student id */
$studentIds = array_keys($attendaceDetails);
/* Loop all student ids and check whether he was present for the working days or not */
foreach($studentIds as $studentId){
foreach($workingDays as $workingDay){
if(isset($attendaceDetails[$studentId][$workingDay])){
echo 'Present';
}else{
echo 'Absent';
}
}
}