大厦的CSV阵列 [英] Building CSV with array
问题描述
我需要运行一个查询,将返回多行,并将其导出为CSV。我必须把细胞按照一定的顺序,但。
I need to run a query that will return multiple rows and export it to a CSV. I have to put the cells in a certain order though.
因此,可以说我的表是奠定了ID,姓名,地址,妻子。我需要的ID,地址,妻子的名字,以建立一个CSV文件。我想我可以只是使以正确的顺序数组,然后做一个CSV与谷歌上搜索,但我不能找出如何使一个CSV一个数组的一个小时之后。
So lets say my table is laid out id, name, address, wife. I need to build a csv in the order of id, address, wife, name. I figured I could just make an array in the correct order and then make a csv with that but after an hour of googling i cant find out how to make a csv with an array.
有是fputcsv但是这需要一个pre-取得CSV。此外,我希望有在做它的codeigniter方式。
There is fputcsv but that requires a pre-made csv. Also, i was hoping there was a codeigniter way of doing it.
public function export() {
$this->load->helper('download');
$data[1] = 'i like pie';
$data[2] = 'i like cake';
force_download('result.csv', $data);
}
我试过,但这个错误表示下载助手文件期待一个字符串不是一个数组。
I tried that but the error said the download helper file was expecting a string not an array.
推荐答案
下面是一些code我用......你可以调整你的出口需要的列...
Here's some code I use... you could adjust the columns you need in the export...
注:此CSV直接发送到 PHP://输出
直接写入到输出缓冲区。这意味着你不保存在服务器在任何的.csv文件,它可以处理的更大的文件大小,建立一个巨大的数组,然后通过它试图循环。
Note: This CSV is directly sent to php://output
which writes directly to the output buffer. This means you're not saving any .csv files on the server and it can handle a much larger file size that building a giant array and then trying to loop through it.
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=\"Jobs_".date('M.j.y', $from)."-".date('M.j.y', $to).".csv\"");
header("Pragma: no-cache");
header("Expires: 0");
$handle = fopen('php://output', 'w');
fputcsv($handle, array(
'JobId',
'Template',
'Customer',
'Status',
'Error',
'PDF',
'Run Time',
'Wait Time',
'Server'
));
foreach ($jobs as $jobData) {
fputcsv($handle, array(
$job->getId(),
$job->getTemplate(),
$jobData['customers_firstname'].' '.$jobData['customers_lastname'],
$status,
$error,
$jobData['products_pdfupload'],
$job->getRunTime(),
$job->getWaitTime(),
$jobData['server']
));
}
fclose($handle);
exit;
这应该给你的CSV导出如何工作的一个良好的心理画面。我不使用codeIgniter的文件下载帮手,所以我不能帮你在这一方面。
This should give you a good mental picture of how a CSV export works. I don't use CodeIgniter's file download helper, so I can't help you on that front.
这篇关于大厦的CSV阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!