2014-04-14 47 views
0

我在Codeigniter制作一个内容管理网站,但是当我用foreach读出我的数组时,它显示了顶部的最旧记录。如何让我的CMS网站显示最新的记录?

我该如何使最新的记录始终处于最佳状态。

这是我的控制器中的功能之一。

function comments($page){ 
    $this->load->library('pagination'); 

    $this->load->model("model_get"); 

    $config['base_url'] = base_url('main/comments/'.$this->uri->segment(3).'/'); 
    $config['total_rows'] = $this->model_get->getCommentcount(); 
    $config['per_page'] = 5; 
    $config['uri_segment'] = 4; 
    $this->pagination->initialize($config); 

    $data["results"] = $this->model_get->getComments($config['per_page'], $page); 
    $this->load->view('content_comment', $data); 
} 

这是我的模型来获取数据。

function getComments($limit, $start){ 
     $this->db->from('comments'); 
     $this->db->where('blog_id', $this->uri->segment(3)); 
     $this->db->limit($limit, $this->uri->segment(4)); 
     $results = $this->db->get(); 

     if ($results) 
     { 
      return $results->result(); 
     }else{ 

     return FALSE; 
     } 
    } 

这是我的foreach显示结果。

<?php 
    foreach($results as $row){ 
     $text = $row->text; 
     echo "<h3>". $text ."</h3>"; 
     $author = $row->author; 
     echo "<p>". $author ."</p>"; 
    } 

回答

1

此行添加到您的模型函数后, “限” 行:

$this->db->order_by("blog_id", "desc"); 
+0

非常感谢!我怎么能看不到。 ;) – Stagg

相关问题