Created
March 25, 2016 21:23
-
-
Save theJasonJones/d47a230d49dabf73ea97 to your computer and use it in GitHub Desktop.
Export CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//might want a check here to make sure the values you need are available | |
require_once('wp-load.php'); | |
global $wpdb; | |
//this function is just taking the $_REQUEST values and returning $wpdb-get_results() | |
$volunteers = jb_get_volunteers(); | |
$header_line = array(); | |
//I just have this to count the number of results. | |
//NOTE: Make sure you do not start this file with "ID" since that can cause "SYLK: File format is not valid" issue in Excel. | |
$header_line[] = '#'; | |
//don't NEED this but usually it is nice to have headers | |
foreach($volunteers[0] as $key =>$value){ | |
$header_line[] = $key; | |
} | |
if(! empty($volunteers)){ | |
// open the "output" stream | |
$fp = fopen('php://output', "w"); | |
//this is putting the first line into the csv file | |
fputcsv($fp, $header_line); | |
foreach($volunteers as $vkey => $volunteer){ | |
$vol_info = array(); | |
$vol_info[] = $vkey; //count of the results | |
foreach($volunteer as $key => $value){ | |
$vol_info[] = $value; | |
} | |
//putting this volunteers info into the csv | |
fputcsv($fp, $vol_info); | |
} | |
} | |
// required for IE, otherwise Content-disposition is ignored | |
if(ini_get('zlib.output_compression')) | |
ini_set('zlib.output_compression', 'Off'); | |
header('Pragma: public'); // required | |
header('Content-Type: text/csv'); | |
header("Content-Transfer-Encoding: Binary"); | |
// tell the browser we want to save it instead of displaying it | |
header('Content-Disposition: attachement; filename="volunter_export_'.date('Ymd').'.csv";'); | |
//there are multiple ways to actually output the info but this seems the cleanest. | |
fpassthru($fp); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment