How to export 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() { $this->load->dbutil(); $this->load->helper('file'); $this->load->helper('download'); $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.
Related Posts
Nagarajan
View Nagarajan's Profile
Latest posts by Nagarajan (see all)
- Random Image viewer using AngularJS - June 30, 2016
- Building your first AngularJS app – Shopping list - June 22, 2016
- How to use Cookies in AngularJS - October 20, 2015
thanks bro…..its simple and easy to understand
but i need your help that how can we make column name bold using code?