Wordpress管理员:从自定义表

时间:2016-06-07 07:47:07

标签: php wordpress csv

我尝试将自定义表格中的某些数据导出为CSV格式。理想情况下,它会在单击按钮时立即下载文件。

通过搜索网络,我想出了很多代码,但无论我做什么,它都不起作用。主要问题是:我得到了所需的输出,但它只是在管理区域内回显,而不是创建可以下载的CSV文件。

我想这是因为我的脚本在WP管理员内部运行,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; 

   }

}

编辑: 根据尼克的代码,我现在尝试了另一种方法。

我将以下内容称为单独的文件:

<?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文件,但它是空的。如果我删除标题内容并使用&#34; echo&#34;而不是&#34; fputcsv&#34;数据在那里。我做错了什么?

1 个答案:

答案 0 :(得分:0)

我认为你不需要回复csvfile。我有类似的方法生成用户列表作为csv,这里是一个有效的代码。事情是,我有一个单独的PHP文件,仪表板上的链接指向单独的PHP文件。单击时,它会生成一个csv并开始下载。您还可以创建自定义页面,然后在functions.php或其他地方编写此函数并预先检查is_page(&#39; target_page&#39;)然后运行此函数,然后重定向回到您所在的位置。

<?php 
// Include WP files so our generator can actually work with WP

$location = $_SERVER['DOCUMENT_ROOT'];

include ($location . '/wp-config.php');
include ($location . '/wp-load.php');
include ($location . '/wp-includes/pluggable.php');

global $wpdb;

function generatecsv() {
  global $wpdb;

     header("Content-type: text/csv");
      header("Content-Disposition: attachment; filename=Daily-Recruiter-CSV.csv");
      header("Pragma: no-cache");
      header("Expires: 0");

      //Functions for gethering data


function how_many_recruiters_do_i_have() {
  global  $wp_query;

  $recruiters = count( get_users( array( 'meta_key' => 'user_role', 'meta_value' => 'recruiter' ) ) );

  $both = count( get_users( array( 'meta_key' => 'user_role', 'meta_value' => 'both' ) ) );

  return $recruiters + $both;
}


function how_many_recruiters_joined_yesterday($date='') {

    global $wpdb;

    if( empty($date) )

    $date = date('Y-m-d');

    $morning = new DateTime($date . ' 00:00:00');

    $night = new DateTime($date . ' 23:59:59'); 


    $m = $morning->format('Y-m-d H:i:s');

    $n = $night->format('Y-m-d H:i:s');


    $sql = $wpdb->prepare("SELECT og_users.* FROM og_users WHERE 1=1 AND CAST(user_registered AS DATE) BETWEEN %s AND %s ORDER BY user_login ASC",$m,$n);

    $users = $wpdb->get_results($sql);

    $recruiter_counter = 0;

    foreach ($users as $user) {

      if ( (get_user_meta($user->ID, 'user_role', true) == 'recruiter') || (get_user_meta($user->ID, 'user_role', true) == 'both') ) {

      $recruiter_counter++;
    }

    }
return $recruiter_counter;

}

function how_many_times_did_recruiters_search() {

  global  $wp_query;

  $recruiters = array();

  $both = array();

  $recruiters = get_users( array( 'meta_key' => 'user_role', 'meta_value' => 'recruiter' ) ) ;

  $both = get_users( array( 'meta_key' => 'user_role', 'meta_value' => 'both' ) ) ;

  $merged_recruiters_array = array_merge($recruiters, $both);

    $search_counter = 0;

  foreach ($merged_recruiters_array as $user_object) {

    $search_counter = $search_counter + get_user_meta($user_object->ID, "search_number", true);

  }

return $search_counter;

}

function how_many_times_recruiter_sent_email() {

  global  $wp_query;

  $recruiters = array();

  $both = array();

  $recruiters = get_users( array( 'meta_key' => 'user_role', 'meta_value' => 'recruiter' ) ) ;

  $both = get_users( array( 'meta_key' => 'user_role', 'meta_value' => 'both' ) ) ;

  $merged_recruiters_array = array_merge($recruiters, $both);

  $mail_counter = 0;

  foreach ($merged_recruiters_array as $user_object) {

    if(strip_tags( get_field( "sent_email_to", 'user_'.$user_object->ID )))
          { 
              $emailsreceived = preg_replace( "/\r|\n/", "", strip_tags( get_field( "sent_email_to", 'user_'.$user_object->ID )));
              $emailreceivedarray = explode(",", $emailsreceived);
              $emailsnumber = count($emailreceivedarray);
              $mail_counter = $mail_counter + ($emailsnumber-1);
          }
    }

    return $mail_counter;
  }


function how_many_times_anyone_sent_email() {

  global  $wp_query;

  $recruiters = array();

  $both = array();

  $talent = array();

  $recruiters = get_users( array( 'meta_key' => 'user_role', 'meta_value' => 'recruiter' ) ) ;

  $both = get_users( array( 'meta_key' => 'user_role', 'meta_value' => 'both' ) ) ;

  $talent = get_users( array( 'meta_key' => 'user_role', 'meta_value' => 'talent' ) ) ;

  $merged_recruiters_array = array_merge($recruiters, $both, $talent);

  $mail_counter = 0;

  foreach ($merged_recruiters_array as $user_object) {

    if(strip_tags( get_field( "sent_email_to", 'user_'.$user_object->ID )))
          { 
              $emailsreceived = preg_replace( "/\r|\n/", "", strip_tags( get_field( "sent_email_to", 'user_'.$user_object->ID )));
              $emailreceivedarray = explode(",", $emailsreceived);
              $emailsnumber = count($emailreceivedarray);
              $mail_counter = $mail_counter + ($emailsnumber-1);
          }
    }

    return $mail_counter;
  }

function how_many_recruiters_logged_in_this_day($date='') {


  global  $wp_query;

  $recruiters = array();

  $both = array();

  $recruiters = get_users( array( 'meta_key' => 'user_role', 'meta_value' => 'recruiter' ) ) ;

  $both = get_users( array( 'meta_key' => 'user_role', 'meta_value' => 'both' ) ) ;

  $merged_recruiters_array = array_merge($recruiters, $both);

  $counter = 0;

  foreach ($merged_recruiters_array as $user_object) {

    if(get_user_meta($user_object->ID, "user_log", true)) {

      if ( strpos(get_user_meta($user_object->ID, "user_log", true), $date)  !== false) {
        $counter++;
      }
    }
  }

return $counter;
}


      //open stream  
      $file = fopen('php://output', 'w');  

      // Add Headers                            
      fputcsv($file, array(
        'Date',
        'How many recruiters I have',
        'How many new recruiters joined today',
        'How many searches the recruiters did',
        'How many times recruiters contacted talent',
        'How many anyone contacted talent',
        'How many recruiters logged into the system'

        )); 

$data = array();

$offset = 30;

$counter = 0;
      while ($counter < $offset ) {
        $counter++;

        $profileRow = array(
            date('d.m.Y',strtotime("-".$counter." days")),
            how_many_recruiters_do_i_have(),
            how_many_recruiters_joined_yesterday( date('Y-m-d',strtotime("-".$counter." days"))),
            how_many_times_did_recruiters_search(),
            how_many_times_recruiter_sent_email(),
            how_many_times_anyone_sent_email(),
            how_many_recruiters_logged_in_this_day(date('d.m.Y',strtotime("-".$counter." days")))



          );
        array_push($data, $profileRow);
      }


     foreach ($data as $row) {

            fputcsv($file, $row);   

     }


      exit(); 
}

generatecsv();

?>