/ Published in: PHP

Expand |
Embed | Plain Text
// Export to CSV if($_GET['action'] == 'export') { $out = ''; // Put the name of all fields for ($i = 0; $i < $columns; $i++) { $out .= '"'.$l.'",'; } $out .="\n"; // Add all values in the table for ($i = 0; $i < $columns; $i++) { $out .='"'.$l["$i"].'",'; } $out .="\n"; } // Output to browser with appropriate mime type, you choose ;) //header("Content-type: text/csv"); //header("Content-type: application/csv"); echo $out; exit; }
Comments

You need to login to post a comment.
In some situations it might also be useful to force the user's browser to download the file. Adding these lines should do the trick.
mysqllistfields is deprecated...
i used this one instead:
[code] mysqlquery("SELECT fieldname1,fieldname2,fieldname3...,fieldnameN FROM tablename"); [/code]
how to make exported stay on server??
@killy, if you want the results to be on the server instead of sent to the client you will want to use fopen() and fwrite() in order to write the contents to a local file (obviously the file/folder will need to be writable by whatever user you are running php as - I believe its generally the apache user, depending on your setup of course).
i'm kinda new to php. i help run a competition with teams from schools all over the country. i've got a page that allows a user to select a variable [a specific school, in this case] from a dropdown. this variable is then part of a query that returns some results specific information about the number of teams from the selected school and outputs them to a table. I'd like to be able to export that same query to csv. Do you all have any thoughts on how I could do that? Thanks much.
i'm kinda new to php. i help run a competition with teams from schools all over the country. i've got a page that allows a user to select a variable [a specific school, in this case] from a dropdown. this variable is then part of a query that returns some results specific information about the number of teams from the selected school and outputs them to a table. I'd like to be able to export that same query to csv. Do you all have any thoughts on how I could do that? Thanks much.