【问题标题】:how to export table data to csv wordpress如何将表格数据导出到 csv wordpress
【发布时间】:2018-06-27 05:28:44
【问题描述】:

我尝试导出用户数据,但出现致命错误 如何将所有用户注册数据提取为 csv/excel 格式 我正在使用此链接的代码 visit here

【问题讨论】:

  • 请用您收到的错误消息更新您的问题。

标签: php mysql wordpress


【解决方案1】:

试试这个,

$header_row = array(
    0 => 'Display Name',
    1 => 'Email',
    2 => 'Institution',
    3 => 'Registration Date',
);
$data_rows = array();
global $wpdb, $bp;
$users = $wpdb->get_results( "SELECT ID, user_email, user_registered FROM {$wpdb->users} WHERE user_status = 0" );
foreach ( $users as $u ) {
    $row = array();
    $row[0] = bp_core_get_user_displayname( $u->ID );
    $row[1] = $u->user_email;
    $row[2] = xprofile_get_field_data( 2, $u->ID );
    $row[3] = $u->user_registered;
    $data_rows[] = $row;
}
$fh = @fopen( 'php://output', 'w' );
fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Description: File Transfer' );
header( 'Content-type: text/csv' );
header( "Content-Disposition: attachment; filename={$filename}" );
header( 'Expires: 0' );
header( 'Pragma: public' );
fputcsv( $fh, $header_row );
foreach ( $data_rows as $data_row ) {
    fputcsv( $fh, $data_row );
}
fclose( $fh );
die();

希望对你有帮助。

如需了解更多信息,

【讨论】:

    【解决方案2】:

    我的解决方案。它们的关键是清理 ob 缓存。相应地更改表和字段类型。

    function Export()
    {
     global $wpdb;
    
     // Use headers so the data goes to a file and not displayed
     header('Content-Type: text/csv');
     header('Content-Disposition: attachment; filename="export.csv"');
    
     // clean out other output buffers
     ob_end_clean();
    
     $fp = fopen('php://output', 'w');
    
     // CSV/Excel header label
     $header_row = array(
        0 => 'Email Address',
        1 => 'First Name',
        2 => 'Last Name',
        );
    
     //write the header
     fputcsv($fp, $header_row);
    
     // retrieve any table data desired. Members is an example 
     $Table_Name   = $wpdb->prefix.'members'; 
     $sql_query    = $wpdb->prepare("SELECT * FROM $Table_Name", 1) ;
     $rows         = $wpdb->get_results($sql_query, ARRAY_A);
     if(!empty($rows)) 
       {
        foreach($rows as $Record)
          {  
          $OutputRecord = array($Record['Email'],
                          $Record['FirstName'],
                          $Record['LastName']);  
          fputcsv($fp, $OutputRecord);       
          }
      }
    
     fclose( $fp );
     exit;                // Stop any more exporting to the file
     }
    

    【讨论】:

    • 是的,你的关键点对我有用。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多