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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探