我在Codeigniter3框架中编写了分页代码,但是当我想转到第二页,第三页等页面时,它显示了404页面。哪里可以问题?我的路线档案:
$route['courses/category/(:any)'] = 'courses/all_courses/$1';
$route['courses/(:any)'] = 'courses/single_course/$1';
我的控制器:
public function all_courses($categories_slug = NULL, $offset = 0) {
//pagination
$config['base_url'] = base_url() . 'courses/category/'.$categories_slug;
$config['total_rows'] = $this->db->count_all('courses');
$config['per_page'] = 2;
$config['uri_segment'] = 4;
$this->pagination->initialize($config);
$data['title'] = 'Bütün Kurslar Burada';
$data['courses'] = $this->courses_model->get_all_courses($categories_slug, $config['per_page'], $offset);
$this->load->view('templates/header');
$this->load->view('courses/courses', $data);
$this->load->view('templates/footer');
}
我的模特:
public function get_all_courses($categories_slug = FALSE, $limit = FALSE, $offset = FALSE) {
if($limit) {
$this->db->limit($limit, $offset);
}
<-- and other codes here -->
}
我的观点:
<div class="col-md-12 courses_page_btn2">
<nav>
<ul class="pagination">
<li><a><?php echo $this->pagination->create_links(); ?> </a></li>
</ul>
</nav>
</div>
答案 0 :(得分:0)
首先,在您的路线文件中,您从课程/类别/(:任意)的路线设置为路线到courses / all_courses / $ 1,但您的控制器的all_courses方法有第二个参数。所以你需要做点什么。我猜测,因为你的分页是使用URI段4,你需要像这样调整你的路线:
$route['courses/category/(:any)/(:num)'] = 'courses/all_courses/$1/$2';
该路由的问题是,如果没有(:num),那么你将获得404.在大多数分页方案中,你将要求它默认为1(第一页)。请改为考虑这条路线:
$route['courses/category/(.+)'] = function ($x, $y = 1)
{
return 'courses/all_courses/' . $x . '/' . $y;
};
这将路由到你的all_courses方法,并提供这两个参数,即使你省略了第二个参数。
这个分页对我有用:
public function all_courses( $x, $y )
{
$this->load->helper('url');
$this->load->library('pagination');
$pconfig['base_url'] = base_url() . 'courses/category/'.$x;
$pconfig['total_rows'] = 10;
$pconfig['per_page'] = 2;
$pconfig['uri_segment'] = 4;
$this->pagination->initialize($pconfig);
echo $this->pagination->create_links();
}