fputcsv()没有添加新行

时间:2017-12-07 05:36:49

标签: php fputcsv

我正在生成我的考勤报告的csv文件,而我正在使用$this->get('login', 'Auth\LoginController@showLoginForm')->name('login'); $this->post('login', 'Auth\LoginController@login'); 函数将其生成为csv文件。我找了学生名单,但没有取回所有学生,只给了我一个学生。

这是我的代码:

->name('')

而不是给我这个结果:

fputcsv()

这就是我得到的:

// 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);

我已经转储+---------------------------------------------------+ | id_no | fullname | gender | dec 1 | dec 2 | dec 3 | | 1 | stud1 | male | A | P | P | | 2 | stud2 | male | P | P | P | ,它有两名学生的记录。谢谢你们!

1 个答案:

答案 0 :(得分:1)

Panther在上面的评论中是正确的,它看起来应该更像这样:

$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];      
}

或者你可以避免使用数组速记语法

$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)); 
}