0

Im am currently working on a blog that displays text below the text box as soon as you post an entry. At the moment my page displays the first 10 entries from the database base with the oldest starting first. How can i make the latest 10 entries display with the latest blog displaying first?

Here is my controller:

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');
    }

   }

Here is my model:

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


}

}

View:

 <?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>

  <?
   }
   ?> 
4

1 回答 1

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();        
}
于 2012-12-13T23:04:30.877 回答