• Resolved mknapp2014

    (@mknapp2014)


    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

    How can I fix the issue?

    Thanks in advance.

    https://www.ads-software.com/plugins/insert-php/

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Export to CSV – Headers Question’ is closed to new replies.