我正在codeigniter的博客网站上工作.其中博客将列在最后一个第一个订单中.最后一个将位于顶部.在我的应用程序变量中,$ blogs包含要列出的所有博客和date_created.$ blogs将显示博客,但不会显示所需的顺序.现在我想按date_created值对博客进行排序.
返回博客的主要模型函数在这里
public function LoadAllBlogs(){
$blogs = array();
$this->db->select('id');
$this->db->from('blog');
$q= $this->db->get();
foreach ($q->result() as $row) {
$blog = new Blog();
$blog->loadWithLatestPublishedEntry($row->id);
$blogs[] = $blog;
}
return $blogs; //contain all the blogs entry and the date in which blog is created
}
Run Code Online (Sandbox Code Playgroud)
谁能帮我.....
您将需要在查询中添加ORDER BY子句,以便按数据库对它们进行排序.它非常简单,将order_by()函数传递给字段名称,asc用于升序或desc用于降序.
$this->db->select('id');
$this->db->from('blog');
$this->db->order_by("date_created", "desc");
$q = $this->db->get();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
738 次 |
| 最近记录: |