We help IT Professionals succeed at work.

Export php content to CSV

alex4544
alex4544 asked
on
Medium Priority
905 Views
Last Modified: 2012-06-27
Hi I want a button on our page that exports the following line to CSV format as that is how it is generated

echo $location.",".$appt['first_name']." ".$appt['last_name'].",".$apdate.",".$aptime.",".$appttype.",".$tel.",2 <br/>";

The code generates something similar to
Home,John Smith,11/11/2009,18:30:00,Appt,07707742558,2
 
Comment
Watch Question

Commented:
Hi there

In order to get the browser to download the data as csv, you need to output http headers before you write the data.  The header you need sets the content disposition, which tells the browser it is a file download and to pop up a save as box:

header("Content-disposition: attachment; filename=myData.csv");

This php must be called BEFORE you output anything to the browser, ie. before you use echo, or output any static html.  You can choose the name of the csv file by changing 'myData.csv'. When you run this code, a dialogue will ask to open or save.  If you click open, excel or openoffice will fire up and display the data.

hth

-null-

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
Since you are exporting for excel, you can use something like the following. It will also do a force download of the file with the xls extension will open in Excel for import when double clicked (on windows)

This is a snippet of code from something I use to export from a database. You'll probably need to make some minor adjustments to take into account the desired file name and the text you wish to output.






    header('Pragma: public'); // required
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: private',false); // required for certain browsers
    header('Content-Type: application/vnd.ms-excel');
    $date = date('Y_m_d');
    $filename = strtolower(str_replace(' ', '_', $site['name'])).'_'.$date.'.xls';
    header('Content-Disposition: attachment; filename='.$filename.';' );
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.strlen($output));
    echo $output;
    exit;

Open in new window

header('Pragma: public'); // required
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: private',false); // required for certain browsers
    header('Content-Type: application/vnd.ms-excel');
    $date = date('Y_m_d');
    $filename = strtolower(str_replace(' ', '_', $site['name'])).'_'.$date.'.xls';
    header('Content-Disposition: attachment; filename='.$filename.';' );
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.strlen($output));
    foreach ($apptResult as $appt) {
      echo $location.",".$appt['first_name']." ".$appt['last_name'].",".$apdate.",".$aptime.",".$appttype.",".$tel.",2";
      echo "\n";
    }
    exit;
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.