I have written the following class to export database results to a CSV file.
<?php
class Export {
public static function tocsv($results = array(), $fields = array()) {
$schema_insert = '"'.implode('","', $fields).'"';
$out .= $schema_insert."\n";
foreach($results as $row) {
$schema_insert = '';
$schema_insert .= '"'.$row->week_ending.'",';
$schema_insert .= '"'.$row->project.'",';
$schema_insert .= '"'.$row->employee.'",';
$schema_insert .= '"'.$row->plots.'",';
$schema_insert .= '"'.$row->categories.'"';
$out .= $schema_insert."\n";
}
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$filename");
echo $out;
exit;
}
}
?>
The output is:
"Week ending","Project name","Plot numbers","Categories","Employee"
"Friday 08 May 2015","Big Road","Tracey Smith","1A, 2A, 3A"," Water meter, 1st fix inc lagging"
However, when I open with Excel everything is in one column. Have I missed something?
Thanks.