PHP简单的csv导出不适用于Wordpress插件中的数组

时间:2016-09-30 05:59:24

标签: php arrays wordpress csv

我想从wordpress插件中导出PHP数组中的csv文件。

然而,当我将die放入使用数组的第一个元素创建的CSV文件的foreach内。否则CSV文件无法生成。

数组:

Array
(
    [0] => Array
        (
            [fname] => test name 1
            [lname] => lname 2
        )
    [1] => Array
        (
            [fname] => test name 2
            [lname] => lname
        )
)

代码:

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

    $filename = "loyalty.csv";
    $handle = fopen('php://output', 'w');

    fputcsv($handle,  array('First Name','last Name'));
    foreach($loUsers as $row) {
        fputcsv($handle, $row);
    //die;
    }
    fclose($handle);

die没有评论时,什么都不会发生,但是如果我放了用第一个元素创建的csv文件。 似乎代码是正确的,无法找到问题。 感谢

2 个答案:

答案 0 :(得分:1)

试试这个:

header("Content-Type: text/csv");
        header("Content-Disposition: attachment; filename=User_Sample.csv");
        // Disable caching
        header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1
        header("Pragma: no-cache"); // HTTP 1.0
        header("Expires: 0"); // Proxies

        $data = array(
            array('User Type', 'User Name', 'Category', 'Mobile Number'),
            array('I', 'Anuj Kumar', 'Building Construction', '8500000001'),
            array('I', 'Arvind Kumar', 'Carpentary', '8500000002'),
            array('I', 'Mridul Ohja', 'Civil Engineering', '8500000003'),
            array('I', 'Naman Kumar', 'Electrical', '8500000004'),
            array('I', 'Sumati', 'Faucets', '8500000005'),
            array('I', 'Anjum', 'Flooring Tiles / Marbles', '8500000006'),
            array('I', 'Rajat', 'Painting', '8500000007'),
            array('C', 'Arvind', 'Plumbing', '8500000008'),
            array('C', 'Rohit', 'Sanitaryware', '8500000009'),
            array('C', 'Gaurav', 'Soil Test Analyst', '8500000010')
        );

        $output = fopen("php://output", "w");
        foreach ($data as $row) {
            fputcsv($output, $row); // here you can change delimiter/enclosure
        }
        fclose($output);

它对我有用。

答案 1 :(得分:1)

查看它很简单:

<?php
    session_start();
    include_once('includes/config.php');
    $filename = "testing-exports.csv";
    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename=$filename");
    header("Pragma: no-cache");
    header("Expires: 0");

    $insquery = "SELECT username FROM r_institution where status=1";
    //$exportstmt = $conn->query($insquery);
    $insresults = $conn->query($insquery);
    //$insresults = mysqli_fetch_assoc($exportstmt);
    foreach ($insresults as $rs) {
        $row = array();
        $row[] = stripslashes($rs["username"]);
        $content[] = $row;
    }
    $content = array();
    $title = array("Institution Emails");
    foreach ($insresults as $rs) {
        $row = array();
        $row[] = stripslashes($rs["username"]);
        $content[] = $row;
    }
    $output = fopen('php://output', 'w');
    fputcsv($output, $title);
    foreach ($content as $con) {
        fputcsv($output, $con);
    }
?>