如何从我的数据库中获取最后10个条目并在我的视图中显示它们,并从最后一个条目开始?

时间:2012-12-13 22:47:55

标签: codeigniter blogs

我正在开发一个博客,只要您发布条目,就会在文本框下方显示文字。目前,我的页面显示数据库库中的前10个条目,其中最早的条目是第一个。如何使用最先显示的最新博客显示最新的10个条目?

这是我的控制器:

class Blog extends CI_Controller {

public function _construct()
{
    parent::__construct();
    $this->load->model("Blogmodel"); 
    $this->load->model("profiles");

}


public function index()
{

    $username = $this->session->userdata('username');

    $id = $this->session->userdata('id');

    $viewData['username'] = $username;

    $this->load->model('Blogmodel');


    if($this->input->post('act') =='create_post')
    {
        $this->Blogmodel->insert_entry();

    }


    $viewData['blogs'] = $this->Blogmodel->get_last_ten_entries();

    $this->load->view('shared/header');  
    $this->load->view('blog/blogtitle', $viewData);
    $this->load->view('shared/nav');
    $this->load->helper('form');// Load the form helper.

    // Lets set the stuff that will be getting pushed forward...
    $data = array();
    $data['form_open']=form_open();
    $data['form_title'] = form_input(array('name' => 'title'));
    $data['form_text'] = form_textarea(array('name' => 'text'));
    $data['form_hidden'] = form_hidden('act','create_post');
    $data['form_submit'] = form_submit('submit','Make Post');


    $this->load->view('blog/post', $data);
    $this->load->view('blog/blogview');


       $this->load->view('shared/footer');
    }

   }

这是我的模特:

class Blogmodel extends CI_Model
 {

 function Blogmodel()
  {
    parent::__construct();
  } 

public function get_last_ten_entries()// this is the function that grabs the entries from my blogs database
{
    $query = $this->db->get('blogs', 10);
    return $query->result();

    }
public function insert_entry()
{
    $this->title = $this->input->post('title');
    $this->body = $this->input->post('text');
    $this->username = $this->session->userdata('username'); 
    $this->date = date("Y-m-d");

    $this->db->insert('blogs', $this);


}

}

查看:

 <?foreach($blogs AS $viewData)
 {
$id = $viewData->id;
$title = $viewData->title;
$body = $viewData->body;
$username = $viewData->username;
$date = $viewData->date;

?>

   <b> <?=$title?></b>
    <p><?=$body?></p>

      <p>posted by:<?=$username?></p>
      <p>date: <?=$date?></p>

<hr>

  <?
   }
   ?> 

1 个答案:

答案 0 :(得分:0)

只需按id字段排序结果(如果表中有一个)

public function get_last_ten_entries()
{
     $this->db->order_by('id', 'DESC');    
     $query = $this->db->get('blogs', 10);
    return $query->result();

 }

或在数据库中添加行时添加时间戳,然后对该列的结果进行排序。

public function insert_entry()
{
    $this->timestamp = time();
    $this->title = $this->input->post('title');
    $this->body = $this->input->post('text');
    $this->username = $this->session->userdata('username'); 
    $this->date = date("Y-m-d");

    $this->db->insert('blogs', $this);   
}

public function get_last_ten_entries()
{
    $this->db->order_by('timestamp', 'DESC');    
    $query = $this->db->get('blogs', 10);
    return $query->result();        
}