使用csv_from_result在CSV导出中返回字符串而不是数据库条目

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()

1) $csv contains all the information, it is all the content that makes up your file. That's why if you don't echo it out you just get a blank CSV file.

2) You don't want to edit $csv, that variable contains everything ready to go. You want to edit $stories, which from the looks of what you have pasted above contains everything from your database that then gets converted into CSV by $this->dbutil->csv_from_result. Look through your code and find where $stories is loaded, then modify that.