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条)

报告相同问题?

悬赏问题

  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题