【发布时间】:2016-06-07 07:47:07
【问题描述】:
我正在尝试将一些数据从自定义表导出到 CSV。理想情况下,它会在单击按钮时立即下载文件。
通过搜索网络,我想出了很多代码,但无论我做什么,它都不起作用。主要问题是:我得到了所需的输出,但它只是在管理区域内得到回显,而不是创建一个可以下载的 CSV 文件。
我猜这是因为我的脚本在 WP admin 中运行,并且 WP 标头在 CSV 标头之前发送。
我现在想到的唯一一件事就是防止这种情况发生:将脚本写入外部文件并在空白浏览器窗口中运行。但后来我在获取要包含的 Wordpress 用户电子邮件和姓名时遇到了麻烦(基于我的自定义表中的 ID)。
现在我正在自定义插件的主文件中运行此代码:
class export_table_to_csv{
private $db;
private $table_name;
private $separator;
function __construct($table_n, $sep, $filename, $eventid){
global $wpdb;
$this->db = $wpdb;
$this->table_name = $table_n;
$this->separator = $sep;
$this->eventid = $eventid;
$generatedDate = date('d-m-Y His');
$csvFile = $this->generate_csv();
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/csv; charset=UTF-8');
header("Content-Disposition: attachment; filename=\"" . $filename . " " . $generatedDate . ".csv\";" );
header("Content-Transfer-Encoding: binary");
header("Content-length: " . filesize($csvFile));
echo $csvFile;
exit;
}
function generate_csv(){
$csv_output = '';
$table = $this->db->prefix . $this->table_name;
$csv_output = $csv_output . "Name". $this->separator ."Email". $this->separator. "Value1". $this->separator ."Value2";
$csv_output .= "\n";
$values = $this->db->get_results("SELECT userid,value1,value2 FROM " . $table . " WHERE eventid=".$this->eventid);
foreach ($values as $rowr) {
$fields = array_values((array) $rowr);
$UserID = $fields[0];
$V1 = $fields[1];
$V2 = $fields[2];
$user_info = get_userdata($UserID);
$first_name = $user_info->first_name;
$last_name = $user_info->last_name;
$Eml = $user_info->user_email;
$Fullname = $first_name." ".$last_name;
$csv_output = $csv_output.$Fullname.$this->separator.$Eml.$this->separator.$V1.$this->separator.$V2;
$csv_output .= "\n";
}
return $csv_output;
}
}
编辑: 根据 Nick 的代码,我现在尝试了另一种方法。
我将以下内容作为单独的文件调用:
<?php
$EventID = $_REQUEST["event"];
$location = $_SERVER['DOCUMENT_ROOT'];
include ($location . '/wp/wp-config.php');
include ($location . '/wp/wp-load.php');
include ($location . '/wp/wp-includes/pluggable.php');
// Name for File
$Loc = get_post_meta($EventID, 'tloc', true);
$Dat = get_post_meta($EventID, 'tdat', true);
$y = substr($Dat, 2, 2);
$m = substr($Dat, 4, 2);
$d = substr($Dat, 6, 2);
$TheDate = $d.$m.$y;
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=Res_".$Loc."_".$TheDate.".csv");
header("Pragma: no-cache");
header("Expires: 0");
$file = fopen('php://output', 'w');
$csv_header .= "Name; Email; Value1; Value2";
$csv_header = "\n";
fputcsv($file, $csv_header);
global $wpdb;
$csv_output = "";
$table_name = $wpdb->prefix . "nameoftable";
$Res = $wpdb->get_results("SELECT userid, value1, value2 FROM $table_name WHERE eventid=$EventID");
foreach ($Res as $rowr) {
$fields = array_values((array) $rowr);
$UserID = $fields[0];
$V1 = $fields[1];
$V2 = $fields[2];
$user_info = get_userdata($UserID);
$Eml = $user_info->user_email;
$first_name = $user_info->first_name;
$last_name = $user_info->last_name;
$Fullname = $first_name." ".$last_name;
$csv_output .= $Fullname;
$csv_output .= "; ";
$csv_output .= $Eml;
$csv_output .= "; ";
$csv_output .= $V1;
$csv_output .= "; ";
$csv_output .= $V2;
$csv_output .= "\n";
fputcsv($file, $csv_output);
$csv_output = "";
}
exit();
?>
现在,一个正确命名的 CSV 文件被下载,但它是空的。如果我删除标题内容并使用“echo”而不是“fputcsv”,则数据就在那里。我做错了什么?
【问题讨论】: