I am using codeigniter to produce a CSV from a database entry. The following code works
$this->load->dbutil();
$csv = $this->dbutil->csv_from_result($stories);
echo $csv;
$name = 'data.csv';
// Build the headers to push out the file properly.
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Disposition: attachment; filename="'.basename($name).'"'); // Add the file name
header('Content-Transfer-Encoding: binary');
header('Connection: close');
exit();
force_download($name, $csv);
fclose($fp);
However, I have two questions.
1) Why does my code rely on echoing $CSV? It returns a blank cvs if I comment out this line.
2) How do I take the $CSV variable and edit it. At the moment my database stores some information as integers that the controllers turn to strings in the normal running of the site i.e. I might store the priority in the database as 0,1,2 but in the view I show Urgent,high,low. I would like to show these strings in the exported CSV rather than the database entry. I am a bit confused as to how I do this. I was thinking of a loop with a switch statement but I dont really know how to interact with the $CSV variable - Or should I be doing this in the model where the data is still in an object rather than csv_from_result()