1

Possible Duplicate:
Create a CSV File for a user in PHP

Even though title looks like simple, my question is a little bit different. I need to export my php table's data into CSV file.

My page has simple form which allows users to choose dates, some of other variables and once user submits the form, I create the table. My form's action is to the same page.

Now I want to add a button, to export data table into a CSV file. I create a PHP function to export the data with the current variables.

I couldn't find anything to call php function without using the forms. But when I use form I lost all of the calculations so that I need to post everything again and do the queries again and again.

Is there any other better way to download the table data ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
CanCeylan
  • 2,890
  • 8
  • 41
  • 51
  • 1
    check here [Create a CSV File for a user in PHP](http://stackoverflow.com/questions/217424/create-a-csv-file-for-a-user-in-php) – lusketeer Jul 18 '12 at 20:35

2 Answers2

1

When you want that people download a file from your site you need to send the correct http header and application file extension to the browser so it can have a slight chance to download it. Then you can echo the mysql table line by line. That way you can download gigabytes of your database. Best way is to do this with an ajax request. There is absolutely no need to create a temporary file and a download link.

Here is a link to a similar question: Create a CSV File for a user in PHP

If you don't like echo you can open a php file handler and use fputcsv together with header command. See the answer from Xenocross.

Community
  • 1
  • 1
Micromega
  • 12,486
  • 7
  • 35
  • 72
0
header ( "Content-Type: application/vnd.ms-excel" );
        header ( "Content-disposition: attachment; filename=Export.csv" );
        header ( "Content-Type: application/force-download" );
        header ( "Content-Transfer-Encoding: binary" );
        header ( "Pragma: no-cache" );
        header ( "Expires: 0" );

        echo ('"data1","data2" \n "data1","data2" ');
Markedagain
  • 106
  • 6