PHP Export to CSV class -
i have written following class export database results csv file.
<?php class export { public static function tocsv($results = array(), $fields = array()) { $schema_insert = '"'.implode('","', $fields).'"'; $out .= $schema_insert."\n"; foreach($results $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 open excel in 1 column. have missed something?
thanks.
use semicolon fieldterminator instead of comma, it's excel thing really, not php thing.
Comments
Post a Comment