php - Building CSV with array -


i need run query return multiple rows , export csv. have put cells in order though.

so lets table laid out id, name, address, wife. need build csv in order of id, address, wife, name. figured make array in correct order , make csv after hour of googling cant find out how make csv array.

there fputcsv requires pre-made csv. also, hoping there codeigniter way of doing it.

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

i tried error said download helper file expecting string not array.

here's code use... adjust columns need in export...

note: csv directly sent php://output writes directly output buffer. means you're not saving .csv files on server , can handle much larger file size building giant array , trying loop through it.

    header("content-type: application/csv");     header("content-disposition: attachment; filename=\"jobs_".date('m.j.y', $from)."-".date('m.j.y', $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 $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 mental picture of how csv export works. don't use codeigniter's file download helper, can't on front.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -