How to use page numbers in Codeigniter pagination instead of offset? How to use page numbers in Codeigniter pagination instead of offset? codeigniter codeigniter

How to use page numbers in Codeigniter pagination instead of offset?


At some point, CI updated their pagination library to (finally) accommodate for this:

$config['use_page_numbers'] = TRUE;

By default, the URI segment will use the starting index for the items you are paginating. If you prefer to show the the actual page number, set this to TRUE.

If you are on a version that does not have this feature and are unable to update your entire CI installation, you should be able to just drop in the latest pagination library.

To calculate your DB offset, use $limit * $page_number where "page_number" is the value in your URL (4th segment in your case).


$limit * $page_number mentioned above didn't return a true offset as a starting point and created problems when the page number is 1 because the offset must be 0 by then. This worked for me:

$offset = ($page_number  == 1) ? 0 : ($page_number * $config['per_page']) - $config['per_page'];


Just use:

$offset = ($this->uri->segment(4)) ? $this->uri->segment(4) : 1;$data['projects'] = $this->Projects_model->get_featured($limit, $limit*($offset-1));