duanhuanbo5225 2014-03-20 12:55
浏览 90
已采纳

使用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条回答 默认 最新

  • duanjunao9348 2014-03-20 13:16
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码