[英]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.
在CodeIgniter中,我需要从数据库中获取最后10个帖子,并将其显示在页面上。
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; 我一直在尝试做一个foreach循环,但仅在使用一行时有效; 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. PS我从代码中删除了所有不必要的内容,例如加载页眉和页脚视图。
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.. 这样很好吗
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.