【问题标题】:Wordpress export data from database to csv using $wpdbWordpress 使用 $wpdb 将数据从数据库导出到 csv
【发布时间】:2017-09-04 06:47:29
【问题描述】:

我被这个问题困住了,我有一个将数据插入数据库的代码,并在 wordpress 页面上查询它。然后我想将其导出为 csv,但我不知道该怎么做。我在互联网上找到了代码但无法正常工作。我不知道为什么,请检查我的代码。这是我的代码..

 global $wpdb;
    if(isset($_POST["Export"])){
    $result = $wpdb->get_results('SELECT * FROM backend_member', ARRAY_A);

    header('Content-Type: text/csv');
    $date = date("Y-m-d H:i:s");
    header('Content-Disposition: attachment;filename=EXPORT_' . $date . '.csv');

    foreach ($result as $row) {
        if ($row) {
            exportCsv(array_keys($row));
        }}
    while ($row) {
        foreach ($result as $row) {
                exportCsv($row);
        }}
    function exportCsv($rows) {
        $separator = '';
        foreach ($rows as $row) {
            echo $separator . $row;
            $separator = ',';
        }
        echo "\r\n";
    }
}

<div><form class="form-horizontal" action="" enctype="multipart/form-data" method="post" name="upload_excel">
<div class="form-group">
<div class="col-md-4 col-md-offset-4"><input class="btn btn-success" name="Export" type="submit" value="export to excel" /></div>
</div>
</form></div>

当我点击导出时。我有一个错误。

This site can’t be reached

The webpage at http://www.rimelig-skat.dk/din-admin/ might be temporarily down or it may have moved permanently to a new web address.
ERR_INVALID_RESPONSE

【问题讨论】:

  • 你把代码放在哪里了?
  • 我把代码放在同一页。
  • @BhumiShah 请帮帮我:(

标签: php wordpress csv


【解决方案1】:

这里是更新的代码:

global $wpdb;
if(isset($_POST["Export"])){
    $filename = 'test';
    $date = date("Y-m-d H:i:s");
    $output = fopen('php://output', 'w');
    $result = $wpdb->get_results('SELECT * FROM     tp_users', ARRAY_A);
    fputcsv( $output, array('ID', 'Title', ' Date'));
    foreach ( $result as $key => $value ) {
        $modified_values = array(
                        $value['ID'],
                        $value['user_login'],
                        $value['user_email']
        );
        fputcsv( $output, $modified_values );
    }
    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: text/csv; charset=utf-8');
    // header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"" . $filename . " " . $date . ".csv\";" );
    // header('Content-Disposition: attachment; filename=lunchbox_orders.csv');
    header("Content-Transfer-Encoding: binary");exit;
}

HTML

<div><form class="form-horizontal" action="" enctype="multipart/form-data" method="post" name="upload_excel">

这段代码在我的 wordpress 中运行良好。

【讨论】:

  • 谢谢你,这是同一个页面吗?或者我需要在function.php 上添加该代码?我会试试这个。
  • 需要在functions.php中添加action
  • 我在wordpress上使用子主题,需要更新什么function.php??
  • 你可以把它放在子function.php上,但是这个按钮在管理面板里吗?
  • 什么意思?我不明白:(
猜你喜欢
  • 1970-01-01
  • 2012-06-27
  • 2013-06-17
  • 2015-06-10
  • 1970-01-01
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多