1条回答 默认 最新
dongqiongzheng0615 2017-11-23 08:44关注Hi the following is one of the ways to achieve the solution your looking out for
NOTE : Just for the sake of demonstration I have included the database query in controller. Make sure you place it in Models folder.
$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']; } } }The above code will create the loop in the following format
$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' ]Now you can loop into the no of working days. For eg
$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'; } } }解决 无用评论 打赏 举报

