manlio Posted June 22, 2015 Posted June 22, 2015 I'm using the following code to generate a csv file (originally taken from a Soma post - thank you! ) and than improved to be read directly from Excel. $array = $items->explode(function($item){ return array( 'user_cardnumber' => $item->user_cardnumber, 'course_type' => $item->course_type->title, 'user_name' => $item->user_name, 'user_surname' => $item->user_surname, 'user_address' => $item->user_address, 'user_zip' => $item->user_zip, 'user_city' => $item->user_city, 'user_email' => $item->user_email ); }); $filepath = "../assets/files/csv/".$itemrichiesta->id.".csv"; $fp = fopen($filepath, 'w'); fprintf($fp, chr(0xEF).chr(0xBB).chr(0xBF));//fix utf-8 encoding for excel foreach ($array as $fields) fputcsv($fp, $fields, ';', ' '); fclose($fp); I would like to add header labels for each field and other additional data at the bottom of the csv file. I'm really new to this and I need help to understand a possible way to follow. I tried with the code below to add some additional info at the bottom of the csv file, but my array knoledge is very limited and I'm not sure what I'm doing wrong. Thank you! $array = $items->explode(function($item){ return array( 'user_cardnumber' => $item->user_cardnumber, 'course_type' => $item->course_type->title, 'user_name' => $item->user_name, 'user_surname' => $item->user_surname, 'user_address' => $item->user_address, 'user_zip' => $item->user_zip, 'user_city' => $item->user_city, 'user_email' => $item->user_email ); }); $array_add = array( 'user_cardnumber' => $user->user_name, 'course_type' => $user->user_surname, 'user_name' => "", 'user_surname' => $user->user_cardnumber, 'user_address' => "", 'user_zip' => "", 'user_city' => "", 'user_email' => "" ); $array = array_push($array, $array_add); $filepath = "../assets/files/csv/".$itemrichiesta->id.".csv"; $fp = fopen($filepath, 'w'); fprintf($fp, chr(0xEF).chr(0xBB).chr(0xBF));//fix utf-8 encoding for excel foreach ($array as $fields) fputcsv($fp, $fields, ';', ' '); fclose($fp);
adrian Posted June 22, 2015 Posted June 22, 2015 Sorry, in a bit of a rush, so I won't answer your question directly, but if you want to generate an export specifically for Excel, I would take a look at making use of something like: https://phpexcel.codeplex.com/ If you want to stay with the CSV route, the Batch Child Editor module (https://processwire.com/talk/topic/6102-batch-child-editor/) might satisfy your needs. It comes with an admin CSV export, and also an API method that can be used like this: // delimiter, enclosure, file extension, names in first row, multiple field separator, array of field names $page->exportCsv(',', '"', 'csv', true, "\r", array('title','body','images','textareas')); This will export the defined fields from all child pages of the current page. Of course I don't know your exact needs, but thought I'd point out these, just in case they are useful. 1
manlio Posted June 24, 2015 Author Posted June 24, 2015 Thank you adrian! I didn't know about Batch Child Editor API ability, but I will give a try to phpexcel because it seems to fullfills my need.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now