我想使用数据库中的数据生成csv文件。如何在PHP CodeIgniter框架中执行此操作?
答案 0 :(得分:1)
使用csv_helper生成csv文件。
按照以下链接获取解决方案,
http://www.code2learn.com/2012/03/generating-csv-file-using-codeigniter.html
答案 1 :(得分:1)
答案 2 :(得分:0)
function query_to_csv( $query, $filename, $attachment = false, $headers = true) {
if($attachment) {
// send response headers to the browser
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);
$fp = fopen('php://output', 'w');
} else {
$fp = fopen($filename, 'w');
}
$result = mysql_query($query);
if($headers) {
// output header row (if at least one row exists)
$row = mysql_fetch_assoc($result);
if($row) {
fputcsv($fp, array_keys($row));
// reset pointer back to beginning
mysql_data_seek($result, 0);
}
}
while($row = mysql_fetch_assoc($result)) {
fputcsv($fp, $row);
}
fclose($fp);
exit;
}