title: Codeigniter Pagination: Run the Query Twice?
body: <p>I'm using codeigniter and the pagination class. This is such a basic question, but I need to make sure I'm not missing something.  In order to get the config items necessary to paginate results getting them from a MySQL database it's basically necessary to run the query twice is that right?</p>
  
  <p>In other words, you have to run the query to determine the total number of records before you can paginate.  So I'm doing it like:</p>
  
  <p>Do this query to get number of results</p>
  
  <pre><code>$this-&gt;db-&gt;where('something', $something);
  $query = $this-&gt;db-&gt;get('the_table_name');
  $num_rows = $query-&gt;num_rows();
  </code></pre>
  
  <p>Then I'll have to do it again to get the results with the limit and offset.  Something like:</p>
  
  <pre><code>$this-&gt;db-&gt;where('something', $something);
  $this-&gt;db-&gt;limit($limit, $offset);
  $query = $this-&gt;db-&gt;get('the_table_name');
  if($query-&gt;num_rows()){
  
      foreach($query-&gt;result_array() as $row){
  
           ## get the results here
      }
  }
  </code></pre>
  
  <p>I just wonder if I'm actually doing this right in that the query always needs to be run twice? The queries I'm using are much more complex than what is shown above.</p>