我想在csv中导出sql表的数据。我正在使用以下代码:
<?php
if(isset($_POST['export']))
{
$file = 'email_csv'; // ?? not defined in original code
$results = $wpdb->get_results("SELECT * FROM `scholarship_uesr`;",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";
$dir = "/home/modifoun/public_html/wp-content/plugins/schloarship/menu-pages/csv/";
$filename = $file."_".date("Y-m-d_H-i",time()).".csv";
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;
// echo "<div class='alert alert-info'>Data Written to CSV File. <a href='".$geturl."' download>Click Here to Download</a></div>";
}
$allsubs = "SELECT * FROM `scholarship_uesr`";
$select_query = $wpdb->get_results($allsubs);
//echo $wpdb->last_error;
?>
<form method="post" action="#" enctype="multipart/form-data">
<input type="submit" name="export" class='btn btn-danger' value="Export Data to CSV !!!">
</form>
当我使用此代码时,它直接打印数据库的数据而不是下载文件。
当我打开error_reporting
时,它会抛出错误:
Warning: Cannot modify header information - headers already sent by (output started at /home/modifoun/public_html/wp-admin/includes/template.php:2041) in /home/modifoun/public_html/wp-content/plugins/schloarship/menu-pages/student_list.php on line 45
Warning: Cannot modify header information - headers already sent by (output started at /home/modifoun/public_html/wp-admin/includes/template.php:2041) in /home/modifoun/public_html/wp-content/plugins/schloarship/menu-pages/student_list.php on line 46
Warning: Cannot modify header information - headers already sent by (output started at /home/modifoun/public_html/wp-admin/includes/template.php:2041) in /home/modifoun/public_html/wp-content/plugins/schloarship/menu-pages/student_list.php on line 47
如何使用wordpress
将数据导出到csv答案 0 :(得分:0)
您需要在输出开始之前运行它。看起来你的模板文件中有这个吗?
尝试将此添加到functions.php文件中。
add_action('init', 'exportCsv');
function exportCsv() {
if(isset($_POST['export']))
{
$file = 'email_csv'; // ?? not defined in original code
$results = $wpdb->get_results("SELECT * FROM `scholarship_uesr`;",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";
$dir = "/home/modifoun/public_html/wp-content/plugins/schloarship/menu-pages/csv/";
$filename = $file."_".date("Y-m-d_H-i",time()).".csv";
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;
// echo "<div class='alert alert-info'>Data Written to CSV File. <a href='".$geturl."' download>Click Here to Download</a></div>";
}
}
答案 1 :(得分:0)
唯一需要的是ob_start()和ob_end_clean()
if if(isset($ _ POST ['export']))使用ob_start()并在if语句的结束括号之前使用ob_end_clean()。
使用标题
标题('内容描述:文件传输');
header(“Content-Type:application / csv”);
header(“Content-Disposition:attachment; filename = Report.csv”);
header(“Pragma:no-cache”);
标题(“过期:0”);