我正在开发一个基于 MVC codeigniter 的项目。我当前的设置是,根据以下查询 (../models/mode.php),它在一个页面中最多显示 10 个表
$function getlist($time)
{
$query = $this->db->query('select * from (select job, username,test from jobs by time desc limit 10 ) as a order by time desc');
.....
}
查询工作正常并在页面中显示 10 个表。虽然数据库有超过 10 个条目,但我想在每个页面中放置 10 个表并合并分页,以便我可以在 10 个不同的页面中看到所有 100 个结果。
在 Controller 中,我有一个部分正在尝试使用分页库,但没有任何帮助。
$this->load->library('pagination');
$config['base_url']= 'http://localhost/pc/index.php/proj/submit/';
$config['uri_segment'] =5;
$config['total_rows']= 100;
$config['per_page'] = 10;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
它所做的只是创建链接,但不允许我遍历不同的页面(当然它会,因为 Controller 不知道它)。我认为 limit 需要有一个开始和结束值,比如 0,100 或开始、结束,但是我怎样才能让 Controller 知道这一点。有任何反馈/建议吗?我是 MVC 世界以及 php/mysql 的新手。
最佳答案
您必须处理模型中的分页:
$function getlist($time, $start, $limit)
{
$query = $this->db->query('select * from (select job, username,test from jobs by time desc limit 10 ) as a order by time desc limit '. $limit .', '. $start);
.....
}
在你的 Controller 中:
// Get pagination page
$page = ($this->uri->segment(5))? $this->uri->segment(5) : 0;
$data = $this->mode_model->getlist($time, $config["per_page"], $page);
关于php - 让分页正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23558537/