CodeIgniter Forums
Exporting current query results to CSV - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Exporting current query results to CSV (/thread-40387.html)



Exporting current query results to CSV - El Forum - 04-07-2011

[eluser]eokorie[/eluser]
Hi,

Is there a way I can export current query results to CSV? I have look through the forums and in most examples, you either have to export the entire table. In my current scenario, I have filtered my data to display a particular set of results and its the filtered output that I want to export as CSV.

Anyone have an idea on achieving this?

Thanks

eokorie


Exporting current query results to CSV - El Forum - 04-07-2011

[eluser]NeoArc[/eluser]
Try

$this->load->dbutil();

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

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

http://ellislab.com/codeigniter/user-guide/database/utilities.html


Exporting current query results to CSV - El Forum - 04-07-2011

[eluser]eokorie[/eluser]
Hi NeoArc,

That just allows you to export the entire contents of a table to a csv. However the data is not filtered! Thanks again however.


Exporting current query results to CSV - El Forum - 04-07-2011

[eluser]NeoArc[/eluser]
You should learn some MySQL Wink

$query = $this->db->query(“SELECT id, name, date, telephone FROM users
WHERE creation_date < '2010-11-10' AND status=1 ORDER BY id DESC LIMIT 20”);