CodeIgniter Forums
exporting query result to csv, one line of code knocking out entire app w/o error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: exporting query result to csv, one line of code knocking out entire app w/o error (/showthread.php?tid=36001)



exporting query result to csv, one line of code knocking out entire app w/o error - El Forum - 11-17-2010

[eluser]Brad K Morse[/eluser]
I created a function that is suppose to export the data table as a csv file

I narrowed it down to $csv= this->data_model->exportCsv(); w/i the controller function export, I commented it out and the app returns to normal, but obviously does not work.

url for this export: domain.com/index.php/admin/export

Code:
// data_model function
function exportCsv() {
    $this->load->dbutil();
    $q = $this->db->order_by('first_name','asc')->get('data');
    return $this->dbutil->csv_from_result($q, ",", "\n");
}

// admin controller function
function export() {
    $this->load->dbutil();
    $this->load->model('data_model');
    $csv= this->data_model->exportCsv(); // this line of the code knocks out my entire ci app
    $file_name = "list.csv";
    force_download($file_name,$csv);
}

Any help as to what I am doing wrong is appreciated.


exporting query result to csv, one line of code knocking out entire app w/o error - El Forum - 11-17-2010

[eluser]Alfredor[/eluser]
You might want to post the data_model method exportCsv() too... as far as I can see you should check if there is anything being returned by the exporCsv() method before processing the download, but that is not a reason for breaking your application.


exporting query result to csv, one line of code knocking out entire app w/o error - El Forum - 11-17-2010

[eluser]Brad K Morse[/eluser]
exportCsv data_model method was posted in the original thread

here it is

Code:
// model function
function exportCsv() {
    $this->load->dbutil();
    $q = $this->db->order_by('first_name','asc')->get('data');
    return $this->dbutil->csv_from_result($q, ",", "\n");
}



exporting query result to csv, one line of code knocking out entire app w/o error - El Forum - 11-17-2010

[eluser]Alfredor[/eluser]
have you tried this method in a little scale? like getting the data of an array and stuff? maybe the method is buffering more resources than your enviroment can afford.


exporting query result to csv, one line of code knocking out entire app w/o error - El Forum - 11-17-2010

[eluser]Brad K Morse[/eluser]
The query returns results, but this cannot be stored in an array, when using force_download(), as it needs to be in a string.


exporting query result to csv, one line of code knocking out entire app w/o error - El Forum - 11-17-2010

[eluser]Alfredor[/eluser]
hmm try to run the exportCsv() at a smaller scale, because from my point of view it has to be with your cvs generation method.


exporting query result to csv, one line of code knocking out entire app w/o error - El Forum - 11-17-2010

[eluser]Brad K Morse[/eluser]
I'm an idiot, I was missing a $ for $csv = this->data_model->exportCsv(); below

Code:
function export() {
    $this->load->dbutil();
    $this->load->model('data_model');
    $csv = this->data_model->exportCsv();
    $file_name = "venue_volunteers.csv";
    force_download($file_name,$csv);
}



exporting query result to csv, one line of code knocking out entire app w/o error - El Forum - 11-17-2010

[eluser]Alfredor[/eluser]
we both are I couldn't spot it either LOL


exporting query result to csv, one line of code knocking out entire app w/o error - El Forum - 11-17-2010

[eluser]Brad K Morse[/eluser]
I appreciate the quick help.