Reports in Codeigniter

Found a nice solution myself. If you want to generate reports in csv format it is very easy with codeigniter.
Your model function

function index(){
return $query = $this->db->get('my_table');
    /*
    Here you should note i am returning 
    the query object instead of 
    $query->result() or $query->result_array()
    */
}    

Now in controller

function get_report(){
    $this->load->model('my_model');
    $this->load->dbutil();
    $this->load->helper('file');
    /* get the object   */
    $report = $this->my_model->index();
    /*  pass it to db utility function  */
    $new_report = $this->dbutil->csv_from_result($report);
    /*  Now use it to write file. write_file helper function will do it */
    write_file('csv_file.csv',$new_report);
    /*  Done    */
}

No externals are required everything is available in codeigntier. Cheers!
If you want to write xml file it is easy too.
Just use xml_from_result() method of dbutil and use write_file('xml_file.xml,$new_report)
Visit these links they will help.

Database Utility Class

And

File Helper

Leave a Comment