I am looking to create a page in my wordpress plugin in which Export MySQL data from table as CSV and download it on click.I have following code
<?php
function fz_csv_export() {
$date = new DateTime();
$ts = $date->format( 'Y-m-d H:i:s' );
$result = array();
$result[] = array(
"first_name" => "Faison", "last_name" => "Zutavern",
"product_viewed" => "Sunglasses", "time_viewed" => $ts
);
$result[] = array(
"first_name" => "Faison", "last_name" => "Zutavern",
"product_viewed" => "Razor", "time_viewed" => $ts
);
$filename = "report-$ts.csv";
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);
$fp = fopen('php://output', 'w');
$hrow = $result[0];
fputcsv($fp, array_keys($hrow));
foreach ($result as $data) {
fputcsv($fp, $data);
}
fclose($fp);
}
// Execute the function
fz_csv_export();
?>
but it prints the data rather than download csv.Any help is appreciate