Search code examples
phpfputcsv

fputcsv() is not adding new row


i'm generating csv file of my attendance report, and i'm using fputcsv() function to generate it to csv file. I looped the list of students but its not retrieving all the students, its only giving me one student.

here's my code:

    // Fetch Students
    $students = $this->db->table('sys_user')
    ->select('*', DB::raw($gc))
    ->leftjoin('sys_user_student', 'sys_user_student.user_id', '=', 'sys_user.user_id')
    ->leftjoin('sys_mf_section', 'sys_mf_section.section_id', '=', 'sys_user_student.section_id')
    ->leftjoin('sys_mf_grade', 'sys_mf_grade.grade_id', '=', 'sys_user_student.grade_id')
    ->leftjoin('gen_access_control_log', 'gen_access_control_log.user_id', '=', 'sys_user.user_id')
    ->where(function($query) use ($search_from,$search_to){
        $query->whereBetween(DB::raw('Date(gen_access_control_log.created_at)'), [$search_from,$search_to]);
    })
    ->where('sys_user.user_type_id', "4")
    ->where('sys_mf_section.section_id', $section_name)
    ->where('sys_mf_grade.grade_id', $grade_name)
    ->where('gen_access_control_log.status', "entrance")
    ->groupby('gen_access_control_log.user_id')
    ->get()->toArray();     


    // ------------------------ EXPORT TO CSV -------------------------//

        // Filename
        $filename = "Attendance ".$search_from." - ".$search_to;

        // Content
        $header = array('Student No.', 'Fullname', 'Gender');
        $head_merge = array_merge($header,$date_array); 

        foreach ($students as $key => $value) {
            $id = $value->identification_number;
            $fullname = $value->last_name.", ".$value->first_name;
            $gender = $value->gender;       
        }

        $list = array (
            $head_merge,
            array($id,$fullname,$gender)
        );

        // Generate CSV
        header('Content-type: application/csv');
        header('Content-Disposition: attachment; filename=' . $filename);
        header("Content-Transfer-Encoding: UTF-8");

        $fp = fopen('php://output', 'w');
        foreach ($list as $key => $value) {
            fputcsv($fp, $value);
        }
        fclose($fp);

instead of giving me this result:

+---------------------------------------------------+ | id_no | fullname | gender | dec 1 | dec 2 | dec 3 | | 1 | stud1 | male | A | P | P | | 2 | stud2 | male | P | P | P |

this is what i get:

+---------------------------------------------------+ | id_no | fullname | gender | dec 1 | dec 2 | dec 3 | | 1 | stud1 | male | A | P | P |

i already dumped $students, it has the records of two students. Thanks guys!


Solution

  • Panther is right in the comment above, it should look more like this:

    $list = [];
    $list[]=$head_merge;
    
    foreach ($students as $key => $value) {
        $id = $value->identification_number;
        $fullname = $value->last_name.", ".$value->first_name;
        $gender = $value->gender; 
        $list[]=[$id,$fullname,$gender];      
    }
    

    Or you could avoid the array-shorthand syntax, that works too

    $list = array();
    array_push($list, $head_merge);
    
    foreach ($students as $key => $value) {
        $id = $value->identification_number;
        $fullname = $value->last_name.", ".$value->first_name;
        $gender = $value->gender; 
        array_push($list, array($id,$fullname,$gender)); 
    }