douju7765 2017-11-23 07:07
浏览 50

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

I have a database table here Database image

enter image description here

I need to display the attendance data in tabular form on monthly basis selection. The output table in php codeigniter should be like: php report

enter image description here

Please help. Thanks a lot.

  • 写回答

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';
            }
        }    
    }
    
    评论

报告相同问题?