Link to home
Start Free TrialLog in
Avatar of alex4544
alex4544

asked on

Export php content to CSV

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
 
ASKER CERTIFIED SOLUTION
Avatar of -null-
-null-

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;