This is an example of generating and displaying pagination in codeigniter.
For Controller
public function display_list(){
$this->load->library('pagination');
$config['base_url'] = base_url().'lists/display_list/'; //your url where the content is displayed
$config['total_rows'] = $this->db->count_all('TABLE_NAME'); //counting the total no of rows in your table
$config['per_page'] = '10';
$config['num_links'] = '5';
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';
$this->pagination->initialize($config);
$data['offset_no']=$this->uri->segment(3);
$this->load->model('model_name');
$data['lists'] = $this->model_name->get_list($config['per_page'],$data['offset_no']);
$this->load->view('lists/display',$data); // the view file name and data to pass
}
For model
function get_list($limit, $offset) {
$this->db->order_by("ID", "asc");
$query = $this->db->get('TABLE_NAME', $limit, $offset);
return $query->result_array();
}
For view
<?php echo $this->pagination->create_links(); //pagination links ?> <?php if(count($lists)>0):?> <table> <tr> <th>Sl.No</th> <th>Field name 1</th> <th>Field name 2</th> <th>Field name 3</th> </tr> <?php for($i=0;$i<count($lists);$i++):?> <tr> <td><?=$i+$offset_no+1?></td> <td><?=$lists[$i]['FIELD_NAME1']?></td> <td><?=$lists[$i]['FIELD_NAME2']?></td> <td><?=$lists[$i]['FIELD_NAME3']?></td> </tr> <?php endfor;?> </table> <?php else:?> <h2>No list to display.</h2> <?php endif;?>