How to export custom database data to excel file
Hi everyone I have the code below, but when I click on the link generated a new page is open with 0 display on it. What I'm doing wrong?
function list_reservation_to_csv() {
global $wpdb;
$table_name = $wpdb-prefix . 'prenotazione_eventi';
$file = 'email_csv'; // ?? not defined in original code
$results = $wpdb-get_results("SELECT * FROM $table_name",ARRAY_A);
if (empty($results)) {
return;
}
$csv_output = '"'.implode('";"',array_keys($results[0])).'";'."\n";;
foreach ($results as $row) {
$csv_output .= '"'.implode('";"',$row).'";'."\n";
}
$csv_output .= "\n";
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
}
add_action('wp_ajax_csv_pull','list_reservation_to_csv');
$ajax_url = admin_url('admin-ajax.php?action=csv_pull');
a href="?php echo $ajax_url; ?"buttonScarica csv/button/a