title: codeigniter CRUD operations with search and user defined per_page
body: <p>I'm designing a page which will have a facility to search and display result into a table with pagination and user has given a freedom to chose number of rows to be displayed per page.</p>
  
  <p>Controller contain following function:</p>
  
  <p><strong>settings_admin.php</strong></p>
  
  <pre><code>    function view_company(){
          $this-&gt;check_logged_in();
          $this-&gt;load-&gt;library('table');
          $this-&gt;load-&gt;library('pagination');
          $fields = $this-&gt;db-&gt;list_fields('company');
          $this-&gt;table-&gt;set_heading($fields);
  
          $field_to_search = 'all';
          if(isset($_POST['company_cols'])){
              $field_to_search = $_POST['company_cols'];
          }
  
          if(isset($_POST['search_text'])){
              $first=true;
              $search_text = $_POST['search_text'];
              if($field_to_search == 'all' &amp;&amp; $search_text!=''){
                  foreach ($fields as $field){
                      if($first){
                          $this-&gt;db-&gt;like($field,$search_text);
                          $first=false;
                      }else{
                          $this-&gt;db-&gt;or_like($field,$search_text);
                      }
                  }
              }else if($search_text!=''){
                  $this-&gt;db-&gt;like($field_to_search,$search_text);
              }
          }
  
          if(isset($_POST['num_of_rows'])){
              $config['per_page'] = $_POST['num_of_rows'];
              $this-&gt;session-&gt;set_userdata('rows_per_page', $_POST['num_of_rows']);
          }else if($this-&gt;session-&gt;userdata('rows_per_page')!=null){
              $config['per_page'] = $this-&gt;session-&gt;userdata('rows_per_page');
          }else{
              $config['per_page'] = 10;
          }
          $result = $this-&gt;db-&gt;get('company',$config['per_page'], $this-&gt;uri-&gt;segment(3));
  
          $config['total_rows'] = $this-&gt;db-&gt;get('company')-&gt;num_rows();
          //$config['total_rows'] = $result-&gt;num_rows();
  
          $config['base_url'] = site_url('/settings_admin/company');
          $config['num_links'] = 5;
          $config['full_tag_open'] = '&lt;div id="pagination"&gt;';
          $config['full_tag_close'] = '&lt;/div&gt;';
          $config['records'] = $result;
          $this-&gt;pagination-&gt;initialize($config);
          //var_dump($config);
          //var_dump($_POST);
          $config['num_of_rows'] = $config['per_page'];
          $this-&gt;load-&gt;view('admin/view/company',$config);
      }
  </code></pre>
  
  <p>View is as follows:</p>
  
  <p><strong>company.php</strong></p>
  
  <pre><code>&lt;?php
      $data = array(
          'title' =&gt; 'Company',
          'form_action' =&gt; 'settings_admin/company'
      );
  ?&gt;
  
  &lt;?php $this-&gt;load-&gt;view('includes/header',$data); ?&gt;
  &lt;?php $this-&gt;load-&gt;view('sidebar_admin_controls'); ?&gt;
  
  &lt;div class="content"&gt;
      &lt;h3&gt;Companies&lt;/h3&gt;
      &lt;div align='right'&gt;
          Search:
          &lt;input style="width:15em;" type="text" name="search_text"
          value="&lt;?php echo set_value('search_text'); ?&gt;" placeholder="search company details"/&gt;
          in
          &lt;select name="company_cols"&gt;
              &lt;option value='all'&gt;all&lt;/option&gt;
              &lt;?php
                  $fields = $this-&gt;db-&gt;list_fields('company');
                  foreach ($fields as $field){
                      echo "&lt;option value='$field'&gt;$field&lt;/option&gt;";
                  }
              ?&gt;
          &lt;/select&gt;
          &lt;input type="submit" name="search" value="Search"/&gt;
      &lt;/div&gt;
      &lt;div align='left'&gt;
          &lt;?php
              if(!isset($num_of_rows)){
                  $num_of_rows = 10;
              }
          ?&gt;
          Rows per page: &lt;input style="width:4em;" type="text" name="num_of_rows" value=&lt;?php echo $num_of_rows;?&gt; placeholder="rows"/&gt;
          &lt;input type="submit" name="search" value="Go"/&gt;
      &lt;/div&gt;
      &lt;div class="view_table" id="view_table"&gt;
          &lt;?php
              echo $this-&gt;table-&gt;generate($records);
          ?&gt;
      &lt;/div&gt;
      &lt;?php
          echo "&lt;br/&gt;";
          echo $this-&gt;pagination-&gt;create_links(); // can run and show me the ok..
          // when i press on page link, it goes back to default #of rows per page (10)
      ?&gt;
      &lt;br/&gt;
      &lt;div align="center" id="option_buttons"&gt;
          &lt;input type="submit" name="add" value="Add New"/&gt;
          &lt;input type="submit" name="update" value="Update"/&gt;
          &lt;input type="submit" name="delete" value="Delete"/&gt;
          &lt;input type="submit" name="print" value="Print"/&gt;
      &lt;/div&gt;
  
  &lt;/div&gt;&lt;!-- end .content --&gt;
  &lt;?php $this-&gt;load-&gt;view('includes/footer'); ?&gt;
  </code></pre>
  
  <p>when I enter number of rows and press <code>GO</code> button, it return with desired pagination but number of page links is not equal to desired ones. e.g. if search returns only one row, it shouls be displayed in table without any page link.But in my case it's displaying single row (search result) along with all page links (equal to total_rows/per_page).</p>
  
  <p>This may be due to the fact that total_rows is been set as <code>$config['total_rows'] = $this-&gt;db-&gt;get('company')-&gt;num_rows();</code> which should actually be number of search result.</p>
  
  <p>But how can I set it if I need to add query parameters as given in code (e.g. 'where' clause)?</p>