创建csv,合并2行,并在codeigniter中将值设置为头

2ul0zpep  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(283)

我正在学习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

我如何才能创建一个csv文件的结果像在codeigniter?或者是否还有其他可能性?
直到现在。我可以在控制器中创建这样的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;
}

我该怎么办?

gab6jxml

gab6jxml1#

这里是一个非常快速和更好的方法,用查询和cidb库创建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);

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

lstz6jyr

lstz6jyr2#

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

相关问题