Pagination handling in Bonfire


Codeigniter provides the pagination library for adding easy pagination to the pages. Bonfire goes one step further in including the library for you in every module you make. So the loading of the pagination library is optional while working with the bonfire module controllers.

Here is a sample code by which pagination can be implemented :

public function index()
    {
        // models loading part
        $this->load->model('users/User_model', 'user_model');
       
        // views control logic
        $results_count = $this->user_model->get_all_users(1);
       
        $sho_per_pg = 2;
        $pag_seg = 5;
        $output['results'] = $this->user_model->get_all_users('',
                                array(
                                    'start' => $this->uri->segment($pag_seg),
                                    'per_page' => $sho_per_pg
                                     )
        );

        $output['toolbar_title'] = 'View Users';

        $this->pagination->initialize(array(
                               'uri_segment' => $pag_seg,
                               'cur_tag_open' => '<b>',
                               'cur_tag_close' => '</b>',
                               'base_url' => '/admin/custom/users/index',
                               'total_rows' => $results_count,
                               'per_page' => $sho_per_pg
                              ));
                             
        $output['pagination'] = $this->pagination->create_links();
        
       
        // rendering views
        Template::set('data',$output);
        Template::set_view('admin/custom/index');
        Template::render();
    }

$results_count counts the total number of results in the query. $sho_per_pg is the number of rows to be shown per page. $pag_seg is the uri segment where the pagination library passes its page number. $output[‘results’] holds the query results after adding the pagination limit. $this->pagination->initialize() function initializes the pagination. $this->pagination->create_links(); creates the links for the pagination. We are holding this pagination html in a variable called $output[‘pagination’] and passing this to the view.
In the admin/custom/index view we would be doing
<?php echo $pagination; ?> to output the pagination to the desired place.