I am trying to convert sql query returned array in CSV file, but i am experiancing a wierd behavior of my code since 1st line of my csv file is coming out to be blanck and also line containing column headers is starting after two blocks. following is my code,
$csv = "name,job,salary,age \n";
$export = $this->model2->emp_data();
foreach ($export as $key => $value) {
$csv.= $value['name'].','. $value['job'].','. $value['sal'].','.$value['age']."\n";
}
$filename ='excel.csv';
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=" . $filename);
header("Pragma: no-cache");
header("Expires: 0");
$csv_handler = fopen('php://output', 'w');
fwrite ($csv_handler,$csv);
fclose ($csv_handler);
$csv is giving me my header names, $export is the sql returned data what am i oing wrong here ? thank you
sample input (query returned data) -
[0] => Array
(
[name] => jeff1
[job] => DBA
[sal] => 6999
[age] => 44
[create] => test
[update] => test
)
[1] => Array
(
[name] => jeff2
[job] => DBA
[sal] => 6999
[age] => 44
[create] => test
[update] => test
)
....so on
output - https://drive.google.com/open?id=1u0Q8OgerLAGlq_r610W7KU0N0hnu0gZE