Reputation: 227
I'm using the pagination class in Codeigniter to page out some info from my database.
It's all working perfectly, however, i'm trying to tidy up the URLs that people use to access the pages.
Currently, i'm pulling out the offset from the URL and have set it to 20 results per page:
http://www.mysite.com/page/20
So if you go to (in this instance) page two, it starts from db entry 20.
The trouble is that firstly, since i've set it to 20 results per page, it means I get odd URLs like http://www.mysite.com/page/60 which would be page 4.
Secondly it's a double figure which i'd like to tidy up.
Basically want I want to do is set the URLs to look like:
http://www.mysite.com/page/2
http://www.mysite.com/page/3
http://www.mysite.com/page/4
etc...
I set the use page numbers to TRUE, but the trouble is it then sets the offset to 2, 3, 4 etc when it should be 20, 40, 60.
Any ideas? I was thinking of some kind of calculation in the model to work out how many entries it should offset based on the individual page number from the URL (when use page numbers is set to TRUE). I can't seem to get it right though...
Upvotes: 1
Views: 2136
Reputation: 1527
In codeigniter number in url means: row per page
So if you need to change per page number with page number change your code like this:
Controller
class news extends CI_Controller{
function index($page = 'page', $page_number = 0){
$row_per_page = 20; // define page per page
// for example if page number == 4 then offset 80
$offset = $page_number * $row_per_page
// we need to request data from database
// we need this query "SELECT * FROM `mytable` limit 80,20"
// offset = 80 AND per page = 20
$this->db->get('mytable', $offser, $row_per_page);
$this->load->library('pagination');
// you can change this url by URI Routing and other solutions.
/*
change application/config/routes.php to nice url
$route['news/page/(:num)'] = "news/index/page/$1";
*/
$config['base_url'] = 'http://example.com/index.php/news/index/page/';
// Change $count_of_rows with your query num_rows you can use SQL_CALC_FOUND_ROWS
$config['total_rows'] = $count_of_wors;
$config['per_page'] = $row_per_page;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
}
}
This is very simple. Enjoy CI
Upvotes: 2