Forums

I need help exporting to csv (1 post)

  1. cheeseslice
    Member
    Posted 1 year ago #

    Hello all,

    I am trying to export data from the 'newsletter' plugin to a csv formatted file for download. I have got the code working on my wamp test server but now that I have uploaded the code to the live site it won't work, the output is just printed to the screen.

    From what I have read the cause of this is having something before the headers but I can't see how I can get around this. What I have done is replace the export page that normally shows the query on screen in a textarea and sent it to a file instead. Here is my code:

    <?php
    
    $file = 'export';
    $query = "select * from " . $wpdb->prefix . "newsletter";
    $recipients = $wpdb->get_results($query . " order by email");
    for ($i=0; $i<count($recipients); $i++) {
    $csv_output .= $recipients[$i]->name . ',' . $recipients[$i]->email . "\r\n";
    } 
    
    $filename = $file."_".date("Y-m-d_H-i",time());
    
    header("Expires: 0");
    header("Cache-control: private");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Description: File Transfer");
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header("Content-disposition: attachment; filename=".$filename.".csv");
    ob_end_clean();
    print $csv_output;
    exit;
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic