简体   繁体   中英

CodeIgniter: return & display multiple rows from DB

Maybe due to lack of sleep I have some problems with my logic. In CodeIgniter, I need to get the last 10 posts from the DB and display them on the page.

My Controller is as follows:

public function index() {

  // $data is an array containing some useless stuff like title, etc.
  // Never mind.

  // Skipped header & navbar view loaders;
  $this->load->view('blog_page', $data);

  $this->get_latest_posts(1);
}

Now the model goes:

public function get_latest_posts($i) {
  // $i is a number of posts to be displayed. Now it is 10
  $query = $this->db->get('posts', $i);
  return $query->result();
  // Any clue what to do here, cause I need an array that contains arrays of post rows;
}

I've been trying to do a foreach cycle, but it only works when using one row; I would appreciate your help, guys!

Every answer will have a positive impact on the life of every kitten on a planet.

PS I removed any unnecessary stuff from my code, like loading header&footer views.

controller function

public function index() {

  // $data is an array containing some useless stuff like title, etc.
  // Never mind.

  // Skipped header & navbar view loaders;
  $this->load->view('blog_page', $data);
  $data['get_latest_post'] = $this->your model name->get_latest_posts(1);
  $this->load->view('your view file name',$data);


}

model function

public function get_latest_posts($i) {

  $query = $this->db->get('posts', $i);
  $this->db->limit(10);

}

is this fine..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM