2016-09-30 21 views
0

我正在从wordpress插件中的PHP数组导出csv文件。PHP简单的CSV导出不适用于Wordpress插件中的数组

但是,当我把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文件与第一个元素创建。 似乎代码是正确的,找不到问题。 感谢

+0

放头与文件编写 –

+1

做你为什么不使用尝试捕捉foreach循环里写出来的只是错误找出最新回事? –

+0

@MichalHainc尝试了尝试但没有任何东西 – Miuranga

回答

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

这对我的作品。

+0

我只是复制你的代码尝试但同样的问题那里,当把死它生成一行csv – Miuranga

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