Creating CSV with Array

I need to run a query that will return multiple rows and export it to CSV. However, I have to put the cells in a specific order.

So, let's say, in my table, id, name, address, wife are indicated. I need to create csv in order id, address, wife, name. I decided that I could just make the array in the correct order, and then make csv with it, but after an hour with googling search I could not find out how to make csv with the array.

There is fputcsv, but this requires a pre-created csv. In addition, I was hoping it would be a codeigniter.

public function export() { $this->load->helper('download'); $data[1] = 'i like pie'; $data[2] = 'i like cake'; force_download('result.csv', $data); } 

I tried this, but the error says that the auxiliary wait file file was expecting the string to not be an array.

+6
source share
3 answers

Here is the code I use ... you can customize the columns you need in the export ...

Note. This CSV is sent directly to php://output , which is written directly to the output buffer. This means that you do not save any CSV files on the server , and it can handle a much larger file size , which creates a giant array, and then tries to skip it.

  header("Content-type: application/csv"); header("Content-Disposition: attachment; filename=\"Jobs_".date('Mjy', $from)."-".date('Mjy', $to).".csv\""); header("Pragma: no-cache"); header("Expires: 0"); $handle = fopen('php://output', 'w'); fputcsv($handle, array( 'JobId', 'Template', 'Customer', 'Status', 'Error', 'PDF', 'Run Time', 'Wait Time', 'Server' )); foreach ($jobs as $jobData) { fputcsv($handle, array( $job->getId(), $job->getTemplate(), $jobData['customers_firstname'].' '.$jobData['customers_lastname'], $status, $error, $jobData['products_pdfupload'], $job->getRunTime(), $job->getWaitTime(), $jobData['server'] )); } fclose($handle); exit; 

This should give you a good idea of ​​how CSV export works. I do not use the CodeIgniter File Upload Assistant, so I cannot help you on this front.

+16
source

This short script creates a CSV and allows you to download it as well:

 function convert_to_csv($input_array, $output_file_name, $delimiter) { /** open raw memory as file, no need for temp files */ $temp_memory = fopen('php://memory', 'w'); /** loop through array */ foreach ($input_array as $line) { /** default php csv handler **/ fputcsv($temp_memory, $line, $delimiter); } /** rewrind the "file" with the csv lines **/ fseek($temp_memory, 0); /** modify header to be downloadable csv file **/ header('Content-Type: application/csv'); header('Content-Disposition: attachement; filename="' . $output_file_name . '";'); /** Send file to browser for download */ fpassthru($temp_memory); } /** Array to convert to csv */ $array_to_csv = Array(Array(12566, 'Enmanuel', 'Corvo'), Array(56544, 'John', 'Doe'), Array(78550, 'Mark', 'Smith')); convert_to_csv($array_to_csv, 'report.csv', ','); 

Here you can read the full post:

PHP Array for CSV - Download

+4
source
 <?php $list = array ( array('aaa', 'bbb', 'ccc', 'dddd'), array('123', '456', '789'), array('"aaa"', '"bbb"') ); $fp = fopen('file.csv', 'w'); foreach ($list as $fields) { fputcsv($fp, $fields); } fclose($fp); ?> 
+2
source

Source: https://habr.com/ru/post/917397/


All Articles