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



我正在字符串中从wordpress插件中的PHP数组导出csv文件。

但是,当我将die放入foreach中时,该CSV文件是用数组的第一个元素创建的。否则不生成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文件。似乎代码是正确的,但找不到问题。感谢

试试这个:

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);

它对我有效。

简单地检查一下:

<?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);
    }
?>

最新更新