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

Posted

技术标签:

【中文标题】创建 CSV 并合并 2 行并将值设置为 codeigniter 中的标题【英文标题】:create CSV with merge 2 rows and set value as header in codeigniter 【发布时间】:2019-03-03 06:32:09 【问题描述】:

我正在学习 codeigniter,尤其是关于导出 CSV 文件,我是否可以创建一个 CSV,将 2 行合并为列并将值设置到标题中

客户:

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

如何在 codeigniter 中创建类似结果的 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;

我该怎么办?

【问题讨论】:

迄今为止尝试过什么?假设每个客户名称:读取相关数据;建行;写csv行 @Barry 我已经添加了我的最后一个代码。但结果和我想要的不一样。 【参考方案1】:

取决于您定义的 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
   ]);

【讨论】:

【参考方案2】:

这是使用查询和 CI db 库创建 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 并合并 2 行并将值设置为 codeigniter 中的标题的主要内容,如果未能解决你的问题,请参考以下文章

合并重复行并设置值

将多个csv文件导入pandas并合并到一个DataFrame中

结合 .csv 并将文件名添加为列

Kettle csv导入,在格式错误的行中设置空值

按 R 中 csv 中的值选择行并将它们保存在新的 csv 文件中

Ruby 在处理大文件时搜索和合并 CSV 文件