I am learning about codeigniter especially about export CSV file, is it possible for me to create an CSV with merge 2 rows become column and set the value into the header
Customers:
ID | CUSTOMER_NAME | TELP | EMAIL
1 | bobby | 698877 | bobby@gmail.com
2 | andrea | 778899 | andrea@gmail.com
Categories:
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
Questions:
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
I want the result for the CSV become like this:
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
How can I create an CSV file the result like that in codeigniter? or if there any other posibility?
until now. i just can create CSV like this in controller
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;
}
What should I do?
Depending on your CSV header define at
fputcsv($handle, [
'customer_name',
'Telp',
'Email
]);
you should pass an array of column but you are passing a single value as a row at each iteration. Try this
$row = []
foreach ($res as $key => $value) {
$row[] = $value;
}
fputcsv($handle, $row);
Or
foreach ($res as $row) {
fputcsv($handle, [
$row->customer_name,
$row->telp,
$row->email
]);
}
Here is really quick and better way for creating csv files with query and CI db library
$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);
columns name will automatically header of your file.