title: Codeigniter Pagination
body: <p>I want to add a drop down to the CodeignIter pagination so that the user could select items per page himself.</p>
  
  <p>But the value of <code>$config['per_page']</code> dosen't change. After I click on page 2 the default value is considered.</p>
  
  <p>Any help how could I accomplish this. in future I also want to add search and sort by name to this grid. So the solution should be an appropriate one.</p>
  
  <p>Here are my files.</p>
  
  <p>Controller:</p>
  
  <pre><code>    &lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  
  class products extends CI_Controller {
  
      public $ipp = 5;
      /**
       * Index Page for this controller.
       *
       * Maps to the following URL
       *      http://example.com/index.php/welcome
       *  - or -  
       *      http://example.com/index.php/welcome/index
       *  - or -
       * Since this controller is set as the default controller in 
       * config/routes.php, it's displayed at http://example.com/
       *
       * So any other public methods not prefixed with an underscore will
       * map to /index.php/welcome/&lt;method_name&gt;
       * @see http://codeigniter.com/user_guide/general/urls.html
       */
      public function __construct(){
         parent::__construct();
         /*if(!$this-&gt;session-&gt;userdata('user_id')){
             redirect(base_url('login'));exit;
         }*/
         $this-&gt;load-&gt;model('productmodel');
         // $this-&gt;load-&gt;model(array('usermodel','categorymodel'));
      }
  
      public function index()
      {
          $config['base_url'] = BASE_URL('products/index');
          $config['total_rows'] = $this-&gt;db-&gt;count_all_results('products');
  
          echo $this-&gt;ipp;
  
          if($this-&gt;input-&gt;post('sel') != ''){
              $this-&gt;ipp = $this-&gt;input-&gt;post('sel');
              $config['per_page'] = $this-&gt;input-&gt;post('sel');
              $this-&gt;pagination-&gt;initialize($config); 
          }else{
              $config['per_page'] = $this-&gt;ipp;
              $this-&gt;pagination-&gt;initialize($config); 
          }
  
          $start = end($this-&gt;uri-&gt;segments);
          $end = $config['per_page'];
          //$start = 0;
  
          if($start == '' || $start == 1){
              $start = 0;
          }
  
          $this-&gt;pagination-&gt;initialize($config);
  
          $data['product_data'] = $this-&gt;productmodel-&gt;getAllProducts($end, $start);
          //$data['categories'] = $this-&gt;cateogorymodel-&gt;getAllCategories();
          //echo '&lt;pre&gt;'; print_r($data);
          if($this-&gt;session-&gt;userdata('username') &amp;&amp; $this-&gt;session-&gt;userdata('password')){
              $this-&gt;load-&gt;view('admin/products/manage', $data);
          }else{
              $this-&gt;load-&gt;view('admin/login');
          }
      }
  
      public function add(){
  
         $this-&gt;load-&gt;view('admin/products/add');
  
      }
  
      public function edit($product_id){
  
         $row = $this-&gt;productmodel-&gt;getProductInfo($product_id);
  
         //var_dump($row-&gt;product_id);
  
         $data['product_id']  = $row-&gt;product_id;
         $data['product_name'] = $row-&gt;product_name;
         $data['product_details'] = $row-&gt;product_details;
         $data['product_price'] = $row-&gt;product_price;
  
         $this-&gt;load-&gt;view('admin/products/add', $data);
  
      }
  
      public function save(){
  
          $this-&gt;load-&gt;library('form_validation');
  
          $this-&gt;form_validation-&gt;set_rules('product_name', 'Product Name', 'trim|required');
          $this-&gt;form_validation-&gt;set_rules('product_details', 'Product Details', 'trim|required');
          $this-&gt;form_validation-&gt;set_rules('product_price', 'Product Price', 'trim|required');
  
          if ($this-&gt;form_validation-&gt;run() == FALSE)
          {
              $this-&gt;load-&gt;view('admin/products/add');
          }
          else
          {
              $product_id = $this-&gt;input-&gt;post('product_id');
  
             $data['product_name'] = $_POST['product_name'];
             $data['product_details'] = $_POST['product_details'];
             $data['product_price'] = $_POST['product_price'];
  
             if($product_id == 0){
                //Add
                $this-&gt;db-&gt;insert('products', $data);
             }else{
                //Edit 
                $this-&gt;db-&gt;where('product_id', $product_id);
                $this-&gt;db-&gt;update('products', $data);    
             }
  
             redirect(BASE_URL('products'));
          }
  
  
  
      }
  
      public function delete($product_id){
         $data['product_id'] = $product_id;
         $this-&gt;db-&gt;delete('products',$data); 
         redirect(BASE_URL('products'));
      }
  
  }
  
  /* End of file welcome.php */
  /* Location: ./application/controllers/welcome.php */
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;?php $this-&gt;load-&gt;view('header'); ?&gt;
  &lt;a href="&lt;?php echo BASE_URL('user/logout'); ?&gt;"&gt;Logout&lt;/a&gt;&lt;br&gt;
  &lt;a href="&lt;?php echo BASE_URL('products/add'); ?&gt;"&gt;Add New Product&lt;/a&gt;
  
  &lt;?php
  echo form_open(BASE_URL('products/index'));
              $options = array(
                              '' =&gt; 'Select',
                               '2' =&gt; '2',
                               '50' =&gt; '50',
                               '100' =&gt; '100');
              echo form_dropdown('sel',$options,'');
  echo form_submit('submit','Submit');
  ?&gt;
  
  &lt;table border="3"&gt;
  
  &lt;tr&gt;
    &lt;td&gt;
      Product ID
    &lt;/td&gt;
    &lt;td&gt;
      Product Name
    &lt;/td&gt;
    &lt;td&gt;
      Product Detail
    &lt;/td&gt;
    &lt;td&gt;
      Product Price
    &lt;/td&gt;
    &lt;td&gt;
      Actions
    &lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;?php 
  for($i = 0;$i&lt;count($product_data);$i++){
  ?&gt;
  &lt;tr&gt;
    &lt;td&gt;
        &lt;?php echo $product_data[$i]['product_id']; ?&gt;
    &lt;/td&gt;
    &lt;td&gt;
        &lt;?php echo $product_data[$i]['product_name']; ?&gt;
    &lt;/td&gt;
    &lt;td&gt;
        &lt;?php echo $product_data[$i]['product_details']; ?&gt;
    &lt;/td&gt;
    &lt;td&gt;
        &lt;?php echo $product_data[$i]['product_price']; ?&gt;
    &lt;/td&gt;
    &lt;td&gt;
      &lt;a href="&lt;?php echo BASE_URL('products/edit/'.$product_data[$i]['product_id']); ?&gt;"&gt;Edit&lt;/a&gt;
      &lt;a href="&lt;?php echo BASE_URL('products/delete/'.$product_data[$i]['product_id']); ?&gt;"&gt;Delete&lt;/a&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  &lt;?php
  }
  ?&gt;
  
  &lt;/table&gt;
  &lt;?php echo $this-&gt;pagination-&gt;create_links(); ?&gt;
  &lt;?php $this-&gt;load-&gt;view('footer'); ?&gt;
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>&lt;?php 
  class productmodel extends CI_Model {
  
      var $title   = '';
      var $content = '';
      var $date    = '';
  
      function __construct()
      {
          // Call the Model constructor
          parent::__construct();
      }
  
      function getAllProducts($limit = '',$start = '')
      {
          if($limit != '' &amp;&amp; $start != '' &amp;&amp; $start != 0){
             $this-&gt;db-&gt;limit($limit, $start);
          }else if($limit != '' &amp;&amp; $start == 0){
             $this-&gt;db-&gt;limit($limit);
          }
          $result = $this-&gt;db-&gt;get('products');
          $data = $result-&gt;result_array();
          return $data;
      }
  
      function getProductInfo($product_id)
      {
          $result = $this-&gt;db-&gt;query('SELECT * FROM products WHERE product_id='.$product_id);
          $row = $result-&gt;row();
          return $row;
      }
  
  }
  
  ?&gt;
  </code></pre>