数据显示如php中的表格形式的月度报告

时间:2017-11-23 07:07:40

标签: php mysql codeigniter

我这里有一个数据库表数据库图像

enter image description here

我需要每月选择以表格形式显示考勤数据。 php codeigniter中的输出表应该是:php report

enter image description here

请帮忙。非常感谢。

1 个答案:

答案 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';
        }
    }    
}