gpt4 book ai didi

php - 创建合并 2 行的 CSV 并将值设置为 codeigniter 中的标题

转载 作者:行者123 更新时间:2023-11-29 16:56:26 24 4
gpt4 key购买 nike

我正在学习 codeigniter,特别是关于导出 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

客户类别关系:

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?

客户答案:

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 文件,其结果与 codeigniter 中的结果类似?或者是否还有其他可能性?

到目前为止。我可以在 Controller 中创建这样的 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);

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

关于php - 创建合并 2 行的 CSV 并将值设置为 codeigniter 中的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52535357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com