创建具有合并 2 行的 CSV,并在代码点火器中将值设置为标题



我正在学习代码点火器,尤其是导出 CSV 文件,我是否可以创建一个合并 2 行成为列并将值设置为标题的 CSV

客户:

ID | CUSTOMER_NAME | TELP   | EMAIL
1  | bobby         | 698877 | bobby@gmail.com
2  | andrea        | 778899 | andrea@gmail.com

类别:

ID | CATERGORY_NAME | DESCRIPTION
1  | Food           | anything about food
2  | Car            | anything about car

Customer_category_relations:

ID | CUSTOMER_ID | CATEGORY_ID
1  | 1           | 1
2  | 2           | 2

问题:

ID | CATEGORY_ID | QUESTION
1  | 1           | what your favorite food?
2  | 1           | which soda do you prefer?
3  | 2           | what sport car do you like?
4  | 2           | have you ever experiance nissan car?

Customer_answers:

ID | CUSTOMER_ID | QUESTION_ID | ANSWER
1  | 1           | 1           | burger
2  | 1           | 1           | salad
3  | 1           | 2           | cocacola
4  | 2           | 3           | mustang
5  | 2           | 3           | Lamborghini 
6  | 2           | 4           | never

我希望 CSV 的结果变成这样:

CUSTOMER_NAME | TELP   | EMAIL            | what your favorite food? | which soda do you prefer? | what sport car do you like? | have you ever experiance nissan car?
bobby         | 698877 | bobby@gmail.com  | burger, salad            | cocacola                  |                             |
andrea        | 778899 | andrea@gmail.com |                          |                           | mustang, Lamborghini        | never

如何在代码点火器中创建类似结果的 CSV 文件? 或者是否有其他可能性?

直到现在。 我只能在控制器中创建这样的CSV

public function export_csv() { 
$filename = "Export_".date("YmdH_i_s").".csv";
header('Content-type:text/csv');
header('Content-Disposition: attachment;filename='.$filename);
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0');
header('Pragma: no-cache');
header('Expires:0');
$handle = fopen('php://output','w');
fputcsv($handle, [
'customer_name',
'Telp',
'Email
]);
$res = $this->db->select('customer.customer_name,
customer.telp,
customer.email,
GROUP_CONCAT('customer_answers.answer) AS answer')
->from('customer')
->join('customer_category_relations', 'customer.id = customer_category_relations.customer_id', 'inner')
->join('categories', 'line_customer_questionnaire_relations.category_id = categories.id', 'INNER')
->join('questions', 'categories.id = questions.category_id', 'INNER')
->join('customer_answers', 'questions.id = customer_answers.question_id', 'INNER') 
->group_by('customer_answers.question_id')
->get()
->result_array();
foreach ($res as $key => $value) {
fputcsv($handle, $value);
}
fclose($handle);
exit;
}

我该怎么办?

这是使用查询和 CI 数据库库创建 csv 文件的非常快速和更好的方法

$query = $this->db->select("customer.customer_name as 'Customer Name',
customer.telp as Telp,
customer.email as Email,
GROUP_CONCAT('customer_answers.answer) AS Answer
")
->from('customer')
->join('customer_category_relations', 'customer.id = customer_category_relations.customer_id', 'inner')
->join('categories', 'line_customer_questionnaire_relations.category_id = categories.id', 'INNER')
->join('questions', 'categories.id = questions.category_id', 'INNER')
->join('customer_answers', 'questions.id = customer_answers.question_id', 'INNER') 
->group_by('customer_answers.question_id')
->get();
$this->load->dbutil();
$csv = $this->dbutil->csv_from_result($query);

列名称将自动成为文件的标题。

根据您的 CSV 标头定义

fputcsv($handle, [
'customer_name',
'Telp',
'Email
]);

您应该传递一个列数组,但在每次迭代时将单个值作为行传递。试试这个

$row = []
foreach ($res as $key => $value) {
$row[] = $value;
}
fputcsv($handle, $row);

foreach ($res as $row) {
fputcsv($handle, [
$row->customer_name,
$row->telp,
$row->email
]);
}

相关内容

  • 没有找到相关文章