I have written pagination code in Codeigniter3 framework, but when I want to go to the second, third and etc. page, it shows me 404 page. Where can be the problem? My routes file:
$route['courses/category/(:any)'] = 'courses/all_courses/$1';
$route['courses/(:any)'] = 'courses/single_course/$1';
My controller:
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');
}
My model:
public function get_all_courses($categories_slug = FALSE, $limit = FALSE, $offset = FALSE) {
if($limit) {
$this->db->limit($limit, $offset);
}
<-- and other codes here -->
}
And my view:
<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>
First, in your routes file, your route from courses/category/(:any) is set to route to courses/all_courses/$1, but your controller's all_courses method has a second parameter. So you need to do something about that. I'm guessing that since your pagination is using URI segment 4 that you'll need to adjust your route like this:
$route['courses/category/(:any)/(:num)'] = 'courses/all_courses/$1/$2';
The problem with that route is that if there is no (:num) then you'll get a 404. In most pagination schemes, you're going to want it to default to 1 (the first page). Consider this route instead:
$route['courses/category/(.+)'] = function ($x, $y = 1)
{
return 'courses/all_courses/' . $x . '/' . $y;
};
This will route to your all_courses method, and provide both parameters, even if you leave out the second one.
This pagination works for me with that route:
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();
}
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.