Excel CSV helper for CodeIgniter

array to csv

$array = array(
	array('Last Name', 'First Name', 'Gender'),
	array('Furtado', 'Nelly', 'female'),
	array('Twain', 'Shania', 'female'),
	array('Farmer', 'Mylene', 'female')
);
 
$this->load->helper('csv');
echo array_to_csv($array);
"Last Name","First Name",Gender
Furtado,Nelly,female
Twain,Shania,female
Farmer,Mylene,female

Direct download

array_to_csv($array, 'toto.csv')

query to csv

$this->load->database();
$query = $this->db->get('my_table');
 
$this->load->helper('csv');
echo query_to_csv($query);

Direct download

query_to_csv($query, TRUE, 'toto.csv')
 

Feedback

Hey can you please help me get the titles for array to csv. I wanna use the array keys as the title (like they do in the query to csv). Can you please tell me how to do it with your helper? mmarcus44@gmail.com
Michael
February 25, 2009
#1
This was a big help to get me going, thanks! In the future I may add in the CI Download Helper usage if anything. @Michael and for anyone else: You can use array_keys and array_values if you need to pull out those pieces of information as a "regular" array with numeric indexes.
Joshua
May 4, 2009
#2
Searching something like this. Thanks Joshua for this great help. :-D
Russel
July 28, 2009
#3
Thanks for this helper. It saved me a lot of work and it's very robust and yet simple to use. I truly appreciate it. Keep up the good work mate!
Rick C.
September 21, 2009
#4
//function that convert an object to an array was useful for mi function objectToArray( $object ) { if( !is_object( $object ) && !is_array( $object ) ) { return $object; } if( is_object( $object ) ) { $object = get_object_vars( $object ); } return array_map( 'objectToArray', $object ); } //hope it helps
uoL
November 11, 2009
#5
Hi can i use this helper and call directly the model function and export it to csv? Thanks please send me to coder5@ymail.com
coder5
January 12, 2010
#6
Beautifully simple, and still works!! Thank you
RJ
January 17, 2010
#7
thank you, very simple and very very useful..
Az
February 7, 2010
#8
Thaks for your helper, it's very simple to use and very useful.

Bur i wonder if i can export the data in a xls format directly. Currently (i implemented this into a client's application), my client has to export the data, open excel, choose import data from file, and select comma separated value.

Anyway, amazing simple helper.

Thanks again
Jesus
February 23, 2010
#9
great

I use this

amazing


mahrizal
April 29, 2010
#10
great

I use this

amazing
Crazy Huy
May 20, 2010
#11
Many thanks! Very handy, saves quite a bit of time! :)
Ethan
June 10, 2010
#12
Thanks for this helper.But I have some problem when I convert to csv file my session have been not exist I want my session to be alive. How I can do?.
KayZ
July 6, 2010
#13