How to export array to CSV in Codeigniter?

房东的猫 提交于 2020-01-07 02:18:29

问题


 $Data[] = array('x'=> $x, 'y'=> $y, 'z'=> $z, 'a'=> $a);

I want to export this array to CSV. I am using CodeIgniter.


回答1:


You can try this code for your export array to CSV.

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Import extends CI_Controller {

        public function __construct() {
            parent::__construct();
        }
        public function exports_data(){
            $data[] = array('x'=> $x, 'y'=> $y, 'z'=> $z, 'a'=> $a);
             header("Content-type: application/csv");
            header("Content-Disposition: attachment; filename=\"test".".csv\"");
            header("Pragma: no-cache");
            header("Expires: 0");

            $handle = fopen('php://output', 'w');

            foreach ($data as $data) {
                fputcsv($handle, $data);
            }
                fclose($handle);
            exit;
        }
}

I hope it will help you.




回答2:


This Solution is working for me you have to call exportCSV Function with CodeIgniter controller

public function exportCSV(){ 
   // file name 
   $filename = 'users_'.date('Ymd').'.csv'; 
   header("Content-Description: File Transfer"); 
   header("Content-Disposition: attachment; filename=$filename"); 
   header("Content-Type: application/csv; ");
   
//    get data from mysql
//    public function ViewDataa($table, $sel) {
//    $this->db->select($sel);
//    $this->db->from($table);
//    return $this->db->get()->result_array();
//    } 

   $usersData = $this->am->ViewDataa("eml_collection", "name, email, phone, Areyouarealtor");
   
  // CSV header
   $header = array("Name","Email","Phone","Areyouarealtor"); 




$usersData   //Will your Data array  
   // file creation 
   $file = fopen('php://output', 'w');
   fputcsv($file, $header);
   foreach ($usersData as $key=>$line){ 
     fputcsv($file,$line); 
   }
   fclose($file); 
   exit; 
  }



回答3:


function exportEtpCsv(){
        $data[] = array('f_name'=> "Nishit", 'l_name'=> "patel", 'mobile'=> "999999999", 'gender'=> "male");
        header("Content-type: application/csv");
        header("Content-Disposition: attachment; filename=\"test".".csv\"");
        header("Pragma: no-cache");
        header("Expires: 0");

        $handle = fopen('php://output', 'w');
        fputcsv($handle, array("No","First Name","Last Name"));
        $cnt=1;
        foreach ($data as $key) {
            $narray=array($cnt,$key["f_name"],$key["l_name"]);
            fputcsv($handle, $narray);
        }
            fclose($handle);
        exit;
    }



回答4:


Best Use is use csv_from_result()

It Permits you to generate a CSV file from a query result. The first parameter of the method must contain the result object from your query.

$this->load->dbutil();

$query = $this->db->query("SELECT * FROM mytable");

echo $this->dbutil->csv_from_result($query);

reference : https://www.codeigniter.com/user_guide/database/utilities.html#export-a-query-result-as-a-csv-file



来源:https://stackoverflow.com/questions/38910297/how-to-export-array-to-csv-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!