Ad Code



How to create pagination in codeigniter

Codeigniter has a native library for the pagination of view page, you can call the pagination library and set the basic configuration and run the application. It is 100% customizable.

Initialize library


this is the library losing method and it is called in the controller function in which view page exists and working.

this is a simple config code.

$config['base_url'] = '';
$config['total_rows'] = 200; //you can set here total recard in table
$config['per_page'] = 20; // no of recard show on the page
$link =  $this->pagination->create_links(); // this is generate ul list for the pagination

The above example is simple pagination.

$config['base_url'] = "" :- This is a base url of current page for every page number sets with page no appended.

$config['total_rows'] = 200 :- This setting use to define total no of record which is paginated by the paginator.

$config['per_page'] = 20 :- This is use to define in a single page how much data list shows.

Setup advance pagination using bootstrap classes

The below example using advanced configuration to setup bootstrap classes to design the pagination list show on the view page.

$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['num_tag_open'] = '<li class="page-item">';
$config['num_tag_close'] = '</li>';
//next and prev link
$config['prev_link'] = '<span aria-hidden="true">&laquo;</span>';
$config['next_link'] = '<span aria-hidden="true">&raquo;</span>';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
//end tag
$config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['next_tag_open'] = '<li class="page-item">';
$config['next_tagl_close'] = '</a></li>';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tagl_close'] = '</li>';
$config['first_tag_open'] = '<li class="page-item disabled">';
$config['first_tagl_close'] = '</li>';
$config['last_tag_open'] = '<li class="page-item">';
$config['last_tagl_close'] = '</a></li>';
$config['attributes'] = array('class' => 'page-link');

$config['base_url'] = base_url(); // set your view page base url
$config['total_rows'] = $this->model->table_count(); // set the total data count in table
$config['per_page'] = 21; // per page no of item shows
$this->pagination->initialize($config); // initialize config data in pagination library
$this->data['pagination_link'] = $this->pagination->create_links(); // this is the pagination link created by library, its html string of pagination
$this->data['data_list'] = $this->model->data_list($config['per_page'], $this->uri->segment(2));  // the data list by model pass per_page as a limit and segment for offset

this is the full customize pagination for bootstrap classes and user database to pagination list into view page, this code put inside view page controller function.

// print your html list or table using $data_list

<div class="row">
     // this variable print pagination html
     <?php if(!empty($pagination_link)){ echo $pagination_link; } ?>

The above code is used for the bootstrap CSS classes to generate pagination but you can customize according to your own CSS classes design and change your look and feel for pagination.

you should know the uri segment number because of the view page open uri segment not available if pagination link click then it will show the segment in URL which is used as an offset in sql like query.

One other things if $config['per_page'] is less then $config['total_rows'] then pagination generate and show otherwise it not show in the view page.

Post a Comment


Ad Code