Dynamic Export to CSV

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Dynamic Export to CSV

Post by marnieg »

I have a page in my website that dynamically echos data from several tables and uses literals. I would like the user to have the option of export this data to a CSV so they can open it in their version of Excel. All of the examples I have seen for fputcsv or other functions only export data from a specific table or array. Is there a way to export this data to a csv file?

Here is what the page looks like with the data echoed.

Link removed

Or maybe there is another function I'm not aware of to open a file and write out line by line the data displayed.
ale8oneboy
Forum Newbie
Posts: 5
Joined: Wed May 19, 2010 9:53 am

Re: Dynamic Export to CSV

Post by ale8oneboy »

Not exactly sure what you're looking for. But If you're looking to export the data as CSV. You could try changing the header to CSV and then echo everything out line by line.

Example:

<?php

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename="myfile.csv");

//line1
echo "column1,column2,column3\r\n";
//line2
echo "column1,column2,column3\r\n";

//loops or whatever here to echo out line by line

?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Dynamic Export to CSV

Post by AbraCadaver »

marnieg wrote:I have a page in my website that dynamically echos data from several tables and uses literals. I would like the user to have the option of export this data to a CSV so they can open it in their version of Excel. All of the examples I have seen for fputcsv or other functions only export data from a specific table or array. Is there a way to export this data to a csv file?

Here is what the page looks like with the data echoed.

Link removed

Or maybe there is another function I'm not aware of to open a file and write out line by line the data displayed.
Nice list of phone numbers :dubious:
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Re: Dynamic Export to CSV

Post by marnieg »

get an error on the line header("Content-Disposition: attachment; filename="export.csv");


Parse error: syntax error, unexpected T_STRING

Should there be another " here to close this statement. My biggest problem is with PHP syntax, when to use " or ' quotes.
ale8oneboy
Forum Newbie
Posts: 5
Joined: Wed May 19, 2010 9:53 am

Re: Dynamic Export to CSV

Post by ale8oneboy »

My bad.

header("Content-Disposition: attachment; filename='export.csv'");
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Dynamic Export to CSV

Post by mikosiko »

AbraCadaver wrote:
marnieg wrote:I have a page in my website that dynamically echos data from several tables and uses literals. I would like the user to have the option of export this data to a CSV so they can open it in their version of Excel. All of the examples I have seen for fputcsv or other functions only export data from a specific table or array. Is there a way to export this data to a csv file?

Here is what the page looks like with the data echoed.

Link removed

Or maybe there is another function I'm not aware of to open a file and write out line by line the data displayed.
Nice list of phone numbers :dubious:
I' calling all the chicks now :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Dynamic Export to CSV

Post by John Cartwright »

Since your using real people's personal information (and they probably didn't consent to this being public), plus it's not important to see the table.. I've removed the link.
Post Reply