4
votes

I have a blog page of posts that I am trying to paginate using CodeIgniter. The numbering and limiting seem to be working fine, except I keep getting a 404 when I try to travel to another page.

The strange thing is the normal culprits that cause this issue are correct. The baseUrl and the uri_segment.

My controller looks like this:

$config                = array();
$config["base_url"]    = $this->config->site_url("/blog");
$config["total_rows"]  = $this->blog_model->count();
$config["per_page"]    = 2;
$config["uri_segment"] = 2;
$config["num_links"] = round($config["total_rows"] / $config["per_page"]);

$config['use_page_numbers'] = TRUE;

$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;

$this->load->view('blog', array(
    'user' => $this->user,
    'blog' => $this->blog_model->loadPosts($config['per_page'], $page),
    'links' => $this->pagination->create_links(),
    'footer' => $this->blog_model->loadFooter()
));

And then in my model I am grabbing the posts

public function loadPosts($limit, $start)
{
    $this->db->limit($limit, $start);
    $this->db->order_by("date", "desc");
    //this loads the contact info
    $query = $this->db->get('entries');
    return $query->result();
}

My full URL is www.mysite.com/blog and then with the pagination it appears as www.mysite.com/blog/2.

For the base_Url I have also tried base_url() . "/blog";.

And I have tried setting the uri_segment to 1 and 3, but nothing seems to work.

As well I have tried playing around with the routing and have added just to see if it would do anything:

$route['blog/(:num)'] = 'blog/$1';
5
You don't need to use uri->segment. Set a parameter in your controller function, it will take the value of your segment. e.g. function example($page = 0). No error possible that way. Also, remove your route rule as it does nothing and may be tricky later. - AdrienXL
@AdrienXL I was at the point of trying anything with the route. I've already disabled it once I saw it did nothing. I've always had to set a uri_segment and I've read many articles that state without it the pagination will not work at all. How would I set it up in the controller without using it? - zazvorniki

5 Answers

5
votes

You can use this line of code if your code is inside the index method:

$route['blog/:any'] = "blog/index/$1";

Because you have used the segment(2), and you should change the blog/index/$1 to blog/:any.

4
votes

Assuming the function name that contain your pagination code is index(), you should change the route to:

$route['blog/(:num)'] = 'blog/index/$1';

And in your index() function, add the $page parameter:

public function index($page = 1){
...
3
votes

With your routes, try if you can to add as many :anys or :nums after as you like:

$route['blog'] = 'blog/index'; // For the first level
$route['blog/(:any)/(:any)'] = 'blog/index/$1/$2'; // For extra "uri" segments.

// base_url pagination

$config["base_url"] = base_url("blog"); // Is preferred
2
votes

You can keep your other code as it is. Just add this at your routes.php file:

$route['blog/:num'] = "blog/index";
//Assumes your code is inside the index method.
//If not, use this way:
//$route['blog/:num'] = "blog/YOUR_METHOD_NAME";
2
votes

You can't pass a parameter to the index() function of a controller like it is a random function.

If you try to do controller/var instead of controller/function/var, CodeIgniter will search for a function var() inside the controller which does not exists. That's what happen when you try to access blog/2: 2() isn't a function in your controller.

You can create a new function in your controller, let's say page(), and move your code inside. That way, you will call blog/page/2. Function page() will exists and will not get a 404. Also don't fordet to redefine your base_url for pagination.

$config["base_url"] = site_url("blog/page");

Another solution if you absolutely need the URL like /blog/2, routes:

$route['blog/(:any)'] = 'blog/index/$1'; 

Remap may also be a solution: http://www.codeigniter.com/user_guide/general/controllers.html#remapping