May 25, 2015

May 25, 2015
In this article, we are going to discuss about How to export the data as CSV from database in CodeIgniter. In CodeIgniter we can export data easily from database as CSV using a library called dbutil. We can pass the query result directly into the dbutil function and we can download the data as CSV.

In your model, write down a function called exportCSV as mentioned below.

function ExportCSV()
    $delimiter = ",";
    $newline = "\r\n";
    $filename = "filename_you_wish.csv";
    $query = "SELECT * FROM table_name WHERE 1";
    $result = $this->db->query($query);
    $data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
    force_download($filename, $data);

You can change the filename and the database query as per your needs. Call this function from your controller.


Post a Comment