Skip to content

Instantly share code, notes, and snippets.

@junaidtk
Created October 22, 2021 04:30
Show Gist options
  • Save junaidtk/6f6bc91abf239364814ab9538755b0ef to your computer and use it in GitHub Desktop.
Save junaidtk/6f6bc91abf239364814ab9538755b0ef to your computer and use it in GitHub Desktop.
Export data as CSV
<?php
$csv_data = generate_csv_file_data($from_date, $to_date);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"report.csv\";" );
header("Content-Transfer-Encoding: binary");
echo $csv_data;
exit;
/**
* Example csv generation.
*/
function generate_csv_file_data(){
$csv_output ='';
$csv_header = array(
'order_id',
'order_key',
'status',
'order_user_name',
);
$i = 0;
foreach($csv_header as $key => $head_name){
$csv_output = $csv_output . $head_name . ",";
$i++;
}
$csv_output .= "\n";
$orders = [1,2,3,4,5,6,7];
$csv_output .= $csv_output . $head_name . ",";
for ($j = 0; $j < $i; $j++) {
$csv_output .= $orders[$j] . 'test orderr'. ",";
}
$csv_output .= "\n";
return $csv_output;
}
Reference Link
https://stackoverflow.com/questions/14160511/export-to-csv-wordpress/31038818
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment