我需要在csv文件中的一个表中导出数据.我能够很好地获取数据,但浏览器不会生成CSV文件.
我的代码是这样的:它的标题问题.我只得到输出与逗号分隔值但不获取csv文件.
/* Converting data to CSV */ public function CSV_GENERATE($getTable) { ob_clean(); global $wpdb; $field=''; $getField =''; if($getTable){ $result = $wpdb->get_results("SELECT * FROM $getTable"); $requestedTable = MysqL_query("SELECT * FROM ".$getTable); // echo "hey";die;//var_dump($result);die; $fieldsCount = MysqL_num_fields($requestedTable); for($i=0; $i<$fieldsCount; $i++){ $field = MysqL_fetch_field($requestedTable); $field = (object) $field; $getField .= $field->name.','; } $sub = substr_replace($getField,'',-1); $fields = $sub; # GET FIELDS NAME $each_field = explode(',',$sub); $csv_file_name = $getTable.'_'.date('Ymd_His').'.csv'; # CSV FILE NAME WILL BE table_name_yyyymmdd_hhmmss.csv # GET FIELDS VALUES WITH LAST COMMA EXCLUDED foreach($result as $row){ for($j = 0; $j < $fieldsCount; $j++){ if($j == 0) $fields .= "\n"; # FORCE NEW LINE IF LOOP COMPLETE $value = str_replace(array("\n","\n\r","\r\n","\r"),"\t",$row->$each_field[$j]); # REPLACE NEW LINE WITH TAB $value = str_getcsv ( $value,","\"","\\"); # SEQUENCING DATA IN CSV FORMAT,required PHP >= 5.3.0 $fields .= $value[0].','; # SEPARATING FIELDS WITH COMMA } $fields = substr_replace($fields,-1); # REMOVE EXTRA SPACE AT STRING END } header("Content-type: text/x-csv"); # DECLARING FILE TYPE header("Content-Transfer-Encoding: binary"); header("Content-Disposition: attachment; filename=".$csv_file_name); # EXPORT GENERATED CSV FILE header("Pragma: no-cache"); header("Expires: 0"); header("Content-type: application/x-msdownload"); //header("Content-Disposition: attachment; filename=data.csv"); return $fields; }
这现在完美地运作了.我们可以将它用作插件.我修改了
this帖子.感谢sruthi sri.
原文链接:https://www.f2er.com/php/135531.html希望这有助于一些:)
<?PHP class CSVExport { /** * Constructor */ public function __construct() { if(isset($_GET['download_report'])) { $csv = $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/octet-stream"); header("Content-Disposition: attachment; filename=\"report.csv\";" ); header("Content-Transfer-Encoding: binary"); echo $csv; exit; } // Add extra menu items for admins add_action('admin_menu',array($this,'admin_menu')); // Create end-points add_filter('query_vars','query_vars')); add_action('parse_request','parse_request')); } /** * Add extra menu items for admins */ public function admin_menu() { add_menu_page('Download Report','Download Report','manage_options','download_report','download_report')); } /** * Allow for custom query variables */ public function query_vars($query_vars) { $query_vars[] = 'download_report'; return $query_vars; } /** * Parse the request */ public function parse_request(&$wp) { if(array_key_exists('download_report',$wp->query_vars)) { $this->download_report(); exit; } } /** * Download report */ public function download_report() { echo '<div class="wrap">'; echo '<div id="icon-tools" class="icon32"> </div>'; echo '<h2>Download Report</h2>'; //$url = site_url(); echo '<p>Export the Subscribers'; } /** * Converting data to CSV */ public function generate_csv() { $csv_output = ''; $table = 'users'; $result = MysqL_query("SHOW COLUMNS FROM ".$table.""); $i = 0; if (MysqL_num_rows($result) > 0) { while ($row = MysqL_fetch_assoc($result)) { $csv_output = $csv_output . $row['Field'].","; $i++; } } $csv_output .= "\n"; $values = MysqL_query("SELECT * FROM ".$table.""); while ($rowr = MysqL_fetch_row($values)) { for ($j=0;$j<$i;$j++) { $csv_output .= $rowr[$j].","; } $csv_output .= "\n"; } return $csv_output; } } // Instantiate a singleton of this plugin $csvExport = new CSVExport();