Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter pagination won't go past first page

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';
like image 321
zazvorniki Avatar asked Dec 05 '22 04:12

zazvorniki


2 Answers

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.

like image 159
Ayed Mohamed Amine Avatar answered Dec 12 '22 12:12

Ayed Mohamed Amine


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){
...
like image 26
JC Sama Avatar answered Dec 12 '22 13:12

JC Sama