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.
$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);
You can change the filename and the database query as per your needs. Call this function from your controller.
Latest posts by Nagarajan (see all)