Welcome Guest, Not a member yet? Register   Sign In
exporting query result to csv, one line of code knocking out entire app w/o error
#1

[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.
#2

[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.
#3

[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");
}
#4

[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.
#5

[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.
#6

[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.
#7

[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);
}
#8

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

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




Theme © iAndrew 2016 - Forum software by © MyBB