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


 public function get_comments ($id) {
  $this->db->order_by('id', 'ASC');  
  $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());
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 广告联盟的兜底广告是什么意思
  • ¥15 如何证明高斯噪声的包络公式
  • ¥150 寻找王者荣耀开发作者,合作或者解答
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化
  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化