Pagination in Codeigniter
Step1:create a database users with fields id,username,city and make necessary changes in the config.php file if required or else you can use the steps mentioned here below http://mydoubts.in/blog/username-validation-using-codeigniter-check-the-username-in-the-databse-for-validating-a-user/ step2:create a controler pagination.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Pagination extends CI_Controller { function __construct() { parent::__construct(); $this->load->model('paginationdata','',TRUE); } function index() { $offset=$this->uri->segment(3); $per_page=3; $config['total_rows'] =$this->paginationdata->countusers(); $this->load->library('table'); $this->load->library('pagination'); $config['base_url'] = base_url().'/index.php/pagination/index'; $config['per_page'] = 3; $config['num_links'] = 5; $this->pagination->initialize($config); $this->db->select('id, username, city'); $data['userslist'] = $this->paginationdata->loadusers($per_page,$offset); $header = array('SINO','Username', 'City'); $this->table->set_heading($header); $this->load->view('mylist', $data); } } ?> |
Step2: create view file mylist.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<? <html> <head> <title>Pagination example using codeigniter</title> </head> <body> <div id='paginationrslt'> <?php echo $this->table->generate($userslist); echo $this->pagination->create_links(); ?> </div> </body> </html> ?> |
Step3: create a model file paginationdata.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php class Paginationdata extends CI_Model { function loadusers($per_page,$offset) { $query = $this->db->get('users', $per_page, $offset); if ($query->num_rows > 0) { return $query; } } function countusers() { return $this->db->count_all('users'); } } ?> |