Title: Export to CSV &#8211; Headers Question
Last modified: August 21, 2016

---

# Export to CSV – Headers Question

 *  [mknapp2014](https://wordpress.org/support/users/mknapp2014/)
 * (@mknapp2014)
 * [11 years, 12 months ago](https://wordpress.org/support/topic/export-to-csv-headers-question/)
 * Hi There –
 * I’ve got a page with php code that exports results of a DB query into CSV.
 * So, the workflow is this:
 * 1. Visitor uses a query form and submits query
    2. User redirected to query results
   page, which has a “Save to CSV” link 3. When they click the link, user redirects
   to a page which executes the CSV creation using the following code:
 *     ```
       [insert_php] 
   
           //Processes the querystring
           $haulIDvar = $_GET['HaulList'];
           $speciesvar = $_GET["SpeciesList"];
           $stationsvar = $_GET["StationList"];
           $datesvar = $_GET["DateList"];
   
       global $wpdb;
   
       //THe if then below evaluates whether or not to export a HAUL WQ Table or a SPECIES Table based on the querystring values
   
       if ($datesvar =="%" && $stationsvar == "%" && $speciesvar == "%" && $haulIDvar != "%")
   
       {
            //Query for Haul Water Quality Table
            $exportList= $wpdb->get_results("SELECT Trawling_Weather_Data.*, Trawling_Logistics.* FROM Trawling_Weather_Data INNER JOIN Trawling_Logistics ON Trawling_Weather_Data.Trawl_Date = Trawling_Logistics.Trawl_Date WHERE Trawling_Weather_Data.HAUL_ID LIKE '" . $haulIDvar . "'" ); 
   
            //processes the query results
   
            //Creates CSV
            // Set header row values
            $csv_fields=array();
            $csv_fields[] = 'Field Name 1';
            $csv_fields[] = 'Field Name 2';
   
       }
   
       else
   
       {
   
       //Query for Species Table
       $exportList= $wpdb->get_results("SELECT Trawling_Species_Data.*, Trawling_Weather_Data.Station FROM Trawling_Species_Data INNER JOIN Trawling_Weather_Data ON Trawling_Species_Data.Haul_ID = Trawling_Weather_Data.HAUL_ID WHERE Trawling_Species_Data.Species LIKE '" . $speciesvar . "' AND Trawling_Weather_Data.Station LIKE '" . $stationsvar . "' AND Trawling_Species_Data.Haul_ID LIKE '" . $haulIDvar . "' AND Trawling_Weather_Data.Trawl_Date LIKE '" . $datesvar . "'ORDER By Trawl_Date DESC" ); 
   
       //processes the query results
       //Creates CSV
            // Set header row values
            $csv_fields=array();
            $csv_fields[] = 'Field Name 1';
            $csv_fields[] = 'Field Name 2';
   
       }
            $output_filename = 'MyDataTable.csv';
            $output_handle = @fopen( 'php://output', 'w' );
   
            header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
            header( 'Content-Description: File Transfer' );
            header( 'Content-type: text/csv' );
            header( 'Content-Disposition: attachment; filename=' . $output_filename );
            header( 'Expires: 0' );
            header( 'Pragma: public' );
   
            // Insert header row
            fputcsv( $output_handle, $csv_fields );
   
            // Parse results to csv format
            foreach ($exportList as $Result) {
       	$leadArray = (array) $Result; // Cast the Object to an array
       	// Add row to file
       	fputcsv( $output_handle, $leadArray );
       	}
   
            // Close output file stream
            fclose( $output_handle ); 
   
       [/insert_php]
       ```
   
 * The problem is that the page is generating the ubiquitous “**Warning: Cannot 
   modify header information**“
 * Here’s an example: [Link to result](http://bristolbayecohealth.org/?page_id=495&HaulList=%&SpeciesList=Asterias%20amurensis&StationList=%&DateList=%)
 * How can I fix the issue?
 * Thanks in advance.

Viewing 1 replies (of 1 total)

 *  Thread Starter [mknapp2014](https://wordpress.org/support/users/mknapp2014/)
 * (@mknapp2014)
 * [11 years, 12 months ago](https://wordpress.org/support/topic/export-to-csv-headers-question/#post-4815228)
 * I’ve since moved this post over to the “insert_php” forum, so if an admin runs
   across this, please delete… it’s redundant.

Viewing 1 replies (of 1 total)

The topic ‘Export to CSV – Headers Question’ is closed to new replies.

## Tags

 * [csv](https://wordpress.org/support/topic-tag/csv/)
 * [export](https://wordpress.org/support/topic-tag/export/)
 * [header](https://wordpress.org/support/topic-tag/header/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [mknapp2014](https://wordpress.org/support/users/mknapp2014/)
 * Last activity: [11 years, 12 months ago](https://wordpress.org/support/topic/export-to-csv-headers-question/#post-4815228)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
