Welcome Guest, Not a member yet? Register   Sign In
Doing some logic for exporting CSV
#1

[eluser]Hantar[/eluser]
I have a table in my DB with following headers: user, category, title, marks And values in it are like this

Code:
User1 - Food - Pizza - 19
User2 - Drinks - Cola - 18
User2 - Food - Spaghetti - 19
User1 - Food - Potato - 15
User1 - Drinks- Pepsi- 10
User2 - Food - Onion - 11
User1 - Food - Tomato - 10

This is also the way it printed in my CSV file, But what I'm trying to achieve in my CSV file is this format

Code:
A(user)        B(=category)        C              D               E
1.  User 1         Food              Pizza -19     Potato - 15     Tomato - 10
2.                 Drinks            Pepsi
3. Empty row
4. User 2          Food              Spaghetti-19   Onion-11
5.                 Drinks            Cola-18      
6. Empty Row
7. A third user with his category's

This basically what I'm trying to achieve - Sorting the user then sorting the category's that the user has and then at every category the user has print the titles and the scores. But How can I sort and do logic for putting in a CSV?

I have the following code

Model:

Code:
function CSV(){    
    $this->load->dbutil();
    $this->db->select('user, category, title, score');
    $query = $this->db->get('Table_restaurant_scores');
    return $this->dbutil->csv_from_result($query, ";", "\r\n");
}

Controller:

Code:
function exporting(){
    $this->load->model('restaurant_mdl');
        $this->load->helper('download');
    $name = 'restaurant_scores.csv';
    $data = $this->restaurant_mdl->CSV();
    force_download($name, $data);
}
Can someone help me out with this?
#2

[eluser]CroNiX[/eluser]
Can't all of the sorting be done in your mysql query?

Code:
$query = $this->db
    ->select('user, category, title, score')
    ->order_by('user ASC, category ASC')
    ->get('Table_restaurant_scores');

As far as how to output it in your csv, you'll have to do that manually instead of using the csv helper. Just iterate over $query->result_array() and use logic to do your formatting.




Theme © iAndrew 2016 - Forum software by © MyBB