Welcome Guest, Not a member yet? Register   Sign In
How does the pagination class work?

I'm wondering how the pagination class works.
My code is similiar to this:

class Books extends Controller {
  function __construct() {

  function index() {
    // load pagination class
    $config['base_url'] = base_url().'index.php/books/index/';
    $config['total_rows'] = $this->db->count_all('christian_books');
    $config['per_page'] = '5';
    $config['full_tag_open'] = '<p>';
    $config['full_tag_close'] = '</p>';

    //load the model and get results
    $data['results'] = $this->books_model->get_books($config['per_page'],$this->uri->segment(3));
    // load the HTML Table Class
    $this->table->set_heading('ID', 'Title', 'Author', 'Description');
    // load the view
    $this->load->view('books_view', $data);

class books_model extends Model {
  function __construct(){

  function get_books($num, $offset) {
    $query = $this->db->get('christian_books', $num, $offset);    
    return $query;

The view just makes the table and the pagination links.

I am wondering how "the flow of the pagination" is going here? Does a click on an pagination link call a new mysql query?


Please paste your books_view views source code.

It sends a value (the offset) to the function at the url specified in config. (Which is the same as uri segment 3)

You can access its value by placing a parameter on your index() function:
function index($offset) {

In your case, as you click next, you'll go to:
/books/index/5 // $this->index(5)
/books/index/10 // $this->index(10)
/books/index/15 // $this->index(15)
... and so on.

Its zero based, so going to the url books/ is the same as

I usually create a separate function (not index) that handles the pagination:
public function index()
    public function page($page = 0, $type = '') {
        $count = $this->news_posts->get_count($type);
        $per_page = 3;
        if($type == 'recipe') {
            $per_page = 10;
        $title = 'News';
        $config = array(

// blah blah blah

Theme © iAndrew 2016 - Forum software by © MyBB