Insert PHP
[resolved] Export to CSV - Headers Question (3 posts)

  1. mknapp2014
    Posted 1 year ago #

    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:

        //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[] = 'Field Name 1';
         $csv_fields[] = 'Field Name 2';
    //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[] = '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 ); 

    The problem is that the page is generating the ubiquitous "Warning: Cannot modify header information"

    Here's an example: Link to result

    How can I fix the issue?

    Thanks in advance.


  2. mknapp2014
    Posted 1 year ago #

    I was able to solve this problem by creating a blank page template, then dropping the php code into the page template.

    So, the page template looks like:

    twentyfourteen-child: My Custom Page
    //the php script goes here
  3. WillBontrager
    Plugin Author

    Posted 1 year ago #

    Cool. Glad you got it solved.

    Code running within Insert PHP has limited variable scope. It doesn't have access to variables $wpdb, for example, as the variable is initialized somewhere other than within the [insert_php] - [/insert_php] block.

    For others who might read this, there's more information here:


Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Insert PHP
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic