Wordpress插件开发 - 导出.csv时出错

时间:2016-03-05 23:19:47

标签: php wordpress

我正在尝试创建一个wordpress插件,我需要从数据库中导出一些东西。

    <?php
ob_start();
global $wpdb;
$statements = $wpdb->get_results( "SELECT * FROM wp_paypal_statements ORDER BY id DESC" );

if (isset($_POST["export"])) 
{
    ob_end_clean();

    $csv = "User ID, PayPal Email, Amount \n";//Column headers
        foreach ($statements as $record)
        {
            $csv .= $record->id . ',' . $record->paypal_email. ',' . $record->amount . "\n"; //Append data to csv
        }
        $csv_handler = fopen("php://output", 'w');
        fwrite ($csv_handler,$csv);
        fclose ($csv_handler);

    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=data.csv');

    exit();
}

我正在获取该文件的html源代码。例如:

<!DOCTYPE html>                                                                                                                                                                                                                                                         
<!--[if IE 8]>                                                                                                                                                                                                                                                          
<html xmlns="http://www.w3.org/1999/xhtml" class="ie8 wp-toolbar"  lang="en-US">                                                                                                                                                                                                                                                            
<![endif]-->                                                                                                                                                                                                                                                            
<!--[if !(IE 8) ]><!-->                                                                                                                                                                                                                                                         
<html xmlns="http://www.w3.org/1999/xhtml" class="wp-toolbar"  lang="en-US">                                                                                                                                                                                                                                                            
<!--<![endif]-->                                                                                                                                                                                                                                                            
<head>                                                                                                                                                                                                                                                          
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />                                                                                                                                                                                                                                                           
<title> &lsaquo; WP Plugin Factory London &#8212; WordPress</title>                                                                                                                                                                                                                                                         
<script type="text/javascript"> 

这就是我的代码看起来如何,我没有看到这里有什么问题。有帮助吗?谢谢!

0 个答案:

没有答案