douqiaoru2583 2016-06-03 08:24
浏览 38
已采纳

使用Codeigniter 3仅显示数据库中的最后5条记录

I wanted to get recent records from database limit only to 5. I used the code below but not working. Maybe I missed something or the whole query is wrong. Please guide me. Thanks

Model

 public function get_comments ($id) {
  $this->db->select('*');
  $this->db->order_by('id', 'ASC');  
  $this->db->from('Item_comments');
  $this->db->limit('5');
  $this->db->where(array('checklist_item_id' => $id, 'status' => 1));

  $query = $this->db->get();
  return $query->result_array();
 }

Note: query works fine and get 5 records from db but I want to position the recent into the bottom. Thanks

  • 写回答

5条回答 默认 最新

  • doujiu9172 2016-06-03 08:32
    关注

    To get the last results you need to order them descending

    $this->db->order_by("id", "desc");
    

    Of course you can also use other columns instead of id.

    To order your results ascending:

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

    To reverse the resulting array:

    $results = get_comments();
    $results = array_reverse($results);
    

    Or if you would like to do it in the function:

    return array_reverse($query->result_array());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集