1

我的数据库中有一个带有以下标题的表:user, category, title, marks 其中的值是这样的

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

这也是它在我的 CSV 文件中打印的方式,但是我试图在我的 CSV 文件中实现的是这种格式

     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

这基本上是我想要实现的 - 对用户进行排序,然后对用户拥有的类别进行排序,然后在用户拥有的每个类别中打印标题和分数。但是我怎样才能对放入 CSV 进行排序和逻辑呢?

我有以下代码

型号

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

控制器:

function exporting(){
    $this->load->model('restaurant_mdl');
        $this->load->helper('download');
    $name = 'restaurant_scores.csv';
    $data = $this->restaurant_mdl->CSV();
    force_download($name, $data);
}

有人可以帮我解决这个问题吗?

4

1 回答 1

0

这取决于您如何在模型中获取数据。您需要在查询中添加 order_by 语句,例如

$this->db->order_by("user", "asc");
$this->db->order_by("category", "asc"); 

// Produces: ORDER BY user DESC, category ASC
于 2013-08-14T22:35:38.100 回答