WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. mknapp2014
    Member
    Posted 3 months 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:

    [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://wordpress.org/plugins/insert-php/

  2. mknapp2014
    Member
    Posted 3 months 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:

    <?php
    /*
    twentyfourteen-child: My Custom Page
    */
    
    //the php script goes here
    
    ?>
  3. WillBontrager
    Member
    Plugin Author

    Posted 3 months 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:

    http://www.willmaster.com/software/WPplugins/insert-php-wordpress-plugin-instructions.php#inandofitself

Reply

You must log in to post.

About this Plugin

About this Topic