Thursday, January 27, 2011

Pagination

 Pagination Controller


<?php
 class Pagination extends Controller {
  function Pagination() {
    parent::Controller();
    $this->load->helper('url');
    $this->load->database();
  }

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

    $this->pagination->initialize($config);
       
    //load the model and get results
    $this->load->model('pagination_model');
    $data['results'] = $this->pagination_model->get_users($config['per_page'],$this->uri->segment(3));
       
    // load the HTML Table Class
    $this->load->library('table');
    $this->table->set_heading('ID', 'Name', 'Age', 'Gender','Address');
       
    // load the view
    $this->load->view('pagination', $data);
  }
}





Pagination Model

<?php
class Pagination_model extends Model {
  function Pagination_model(){
    parent::Model();
  }

  function get_users($num, $offset)
  /*$num specifies the number of results to retrieve from the database and
    $offset specifies where it should start retrieving results from.*/
    {
    $query = $this->db->get('user', $num, $offset);   
    return $query;
  }
}

Pagination View

<html>
<head>
<title>CodeIgniter Pagination </title>
</head>
<body>
<h1>Welcome To Codeigniter</h1>
<?php echo $this->table->generate($results); ?>
<?php echo $this->pagination->create_links(); ?>
</body>
</html>




No comments:

Post a Comment