...

View Full Version : Export To CSV



tomyknoker
08-20-2007, 05:27 PM
Yes another csv question, I have found many topics but not what I need, I only want to export 4 columns from my database, and also specify column headings for the column names... Is that possible?

Fumigator
08-20-2007, 08:16 PM
Have a look at fputcsv() (http://us2.php.net/manual/en/function.fputcsv.php)

tomyknoker
08-21-2007, 04:30 PM
Thanks I have read through that page over and over it doesn't really help me though... If ANYONE has a clean way of doing the following would be great. I have read about 25 posts from Google searches every single one different every single one using different PHP elements...

What I want to do is pull out from my table into a csv file 5 columns, FirstName, LastName, Email, Mobile... I then want to add to the top of the .csv seperate headings... So basically I want to achieve this...

Fname, Sname, Email, Mobile
Tom, Smith, tom@smith.com, 0410000000
James, Smith, james@smith.com, 0410000000

I have tried every which way and can't get a result... I'm not sure where to go from this point... How do I write to the actual .csv file? I mean what do I need to add to the above code to get a file?

Fumigator
08-21-2007, 05:13 PM
You read that page over and over but never thought to try the example? It's about as "clean" as you're going to find. Here's the example:



<?php

$list = array (
'aaa,bbb,ccc,dddd',
'123,456,789',
'"aaa","bbb"'
);

$fp = fopen('file.csv', 'w');

foreach ($list as $line) {
fputcsv($fp, split(',', $line));
}

fclose($fp);
?>


Once you see that work, you'll be able to replace the array called $list with your own array built from your database query. I'm not sure how much more straightforward it can get for you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum