Php Wordpress导出到csv未下载
我必须从表中导出数据。单击“导出CSV”按钮我需要下载csv文件。我想在同一页中以表格形式显示输出数据,因此我无法在另一个文件中写入csv导出操作。我已在同一文件中写入导出csv代码。它以逗号分隔的值输出,但不下载csv。这是我的代码Php Wordpress导出到csv未下载,php,wordpress,csv,Php,Wordpress,Csv,我必须从表中导出数据。单击“导出CSV”按钮我需要下载csv文件。我想在同一页中以表格形式显示输出数据,因此我无法在另一个文件中写入csv导出操作。我已在同一文件中写入导出csv代码。它以逗号分隔的值输出,但不下载csv。这是我的代码 global $wpdb; $id = $_POST['id']; $timesheet_entry = $wpdb->get_results( "SELECT * FROM table WHERE id=$id" ); foreach($timesh
global $wpdb;
$id = $_POST['id'];
$timesheet_entry = $wpdb->get_results( "SELECT * FROM table WHERE id=$id" );
foreach($timesheet_entry as $entry){
$csv_export.= $entry->id.",";
$csv_export.= $entry->entry_date.",";
$csv_export.= $entry->user_id.",";
$csv_export.= $entry->hours_worked.",";
$csv_export.= $entry->project_id.",";
$csv_export.= '
';
}
$csv_filename ='export.csv';
// Export the data and prompt a csv file for download
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=".$csv_filename."");
echo($csv_export);
请尝试使用此代码
header("Content-type: application/CSV");
header("Content-Disposition: attachment;filename=" . $csv_filename);
echo $csv_export;
根据您对同一页面的评论,请使用:
$id = $_POST['id'];
if( ! empty($id) ) {
// Rest Code here
// ..............
exit();
}
这种方式可以帮助您在同一页面上实现CSV下载。请澄清,这是浏览器特有的问题吗?你试过在不同的浏览器上下载吗?没有错误消息@Zameer Khan如果我将导出csv代码放在另一个文件中,它工作正常,但在同一个文件中不工作。