<tfoot id='yobib'></tfoot>

    1. <small id='yobib'></small><noframes id='yobib'>

    2. <legend id='yobib'><style id='yobib'><dir id='yobib'><q id='yobib'></q></dir></style></legend>
        <bdo id='yobib'></bdo><ul id='yobib'></ul>
      1. <i id='yobib'><tr id='yobib'><dt id='yobib'><q id='yobib'><span id='yobib'><b id='yobib'><form id='yobib'><ins id='yobib'></ins><ul id='yobib'></ul><sub id='yobib'></sub></form><legend id='yobib'></legend><bdo id='yobib'><pre id='yobib'><center id='yobib'></center></pre></bdo></b><th id='yobib'></th></span></q></dt></tr></i><div id='yobib'><tfoot id='yobib'></tfoot><dl id='yobib'><fieldset id='yobib'></fieldset></dl></div>
      2. 导出到 csv wordpress

        时间:2023-11-30
          <tbody id='KcvIz'></tbody>

        <small id='KcvIz'></small><noframes id='KcvIz'>

          <bdo id='KcvIz'></bdo><ul id='KcvIz'></ul>

            • <i id='KcvIz'><tr id='KcvIz'><dt id='KcvIz'><q id='KcvIz'><span id='KcvIz'><b id='KcvIz'><form id='KcvIz'><ins id='KcvIz'></ins><ul id='KcvIz'></ul><sub id='KcvIz'></sub></form><legend id='KcvIz'></legend><bdo id='KcvIz'><pre id='KcvIz'><center id='KcvIz'></center></pre></bdo></b><th id='KcvIz'></th></span></q></dt></tr></i><div id='KcvIz'><tfoot id='KcvIz'></tfoot><dl id='KcvIz'><fieldset id='KcvIz'></fieldset></dl></div>
              <tfoot id='KcvIz'></tfoot>
              • <legend id='KcvIz'><style id='KcvIz'><dir id='KcvIz'><q id='KcvIz'></q></dir></style></legend>

                1. 本文介绍了导出到 csv wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要将数据导出到 csv 文件的一个表中.我可以正常获取数据,但浏览器没有生成 CSV 文件.

                  I need to export data in one table in a csv file. I'm able to get the data fine but the CSV file is not being generated by the browser.

                  我的代码是这样的:标题的问题.我只得到带有逗号分隔值的输出,但没有得到 csv 文件.

                  My code is like this: its the problem with headers. I'm getting only the output with commas separated values but not getting csv file.

                  /* 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 .= "
                  "; # FORCE NEW LINE IF LOOP COMPLETE
                                  $value = str_replace(array("
                  ", "
                  
                  ", "
                  ", "
                  "), "	", $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.

                  This is working perfectly now. we can use this as a plugin. I modified this post. thanks to sruthi sri.

                  希望这对某人有所帮助:)

                  Hope this helps some one :)

                  <?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', array($this, 'query_vars'));
                  add_action('parse_request', array($this, 'parse_request'));
                  }
                  
                  /**
                  * Add extra menu items for admins
                  */
                  public function admin_menu()
                  {
                  add_menu_page('Download Report', 'Download Report', 'manage_options', 'download_report', array($this, '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 .= "
                  ";
                  
                  $values = mysql_query("SELECT * FROM ".$table."");
                  while ($rowr = mysql_fetch_row($values)) {
                  for ($j=0;$j<$i;$j++) {
                  $csv_output .= $rowr[$j].",";
                  }
                  $csv_output .= "
                  ";
                  }
                  
                  return $csv_output;
                  }
                  }
                  
                  // Instantiate a singleton of this plugin
                  $csvExport = new CSVExport();
                  

                  这篇关于导出到 csv wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHP - 无法修改标头信息 下一篇:PHP用POST数据打开另一个网页

                  相关文章

                    1. <legend id='Y7pJe'><style id='Y7pJe'><dir id='Y7pJe'><q id='Y7pJe'></q></dir></style></legend>

                      <i id='Y7pJe'><tr id='Y7pJe'><dt id='Y7pJe'><q id='Y7pJe'><span id='Y7pJe'><b id='Y7pJe'><form id='Y7pJe'><ins id='Y7pJe'></ins><ul id='Y7pJe'></ul><sub id='Y7pJe'></sub></form><legend id='Y7pJe'></legend><bdo id='Y7pJe'><pre id='Y7pJe'><center id='Y7pJe'></center></pre></bdo></b><th id='Y7pJe'></th></span></q></dt></tr></i><div id='Y7pJe'><tfoot id='Y7pJe'></tfoot><dl id='Y7pJe'><fieldset id='Y7pJe'></fieldset></dl></div>
                    2. <tfoot id='Y7pJe'></tfoot>
                        <bdo id='Y7pJe'></bdo><ul id='Y7pJe'></ul>

                      <small id='Y7pJe'></small><noframes id='Y7pJe'>