title: Codeigniter Pagination From Database limit, offset
body: <p>I started a web application in CI 3.0, everything working smooth, I got my pagination working, but there is a problem wich I cannot figure out...</p>
  
  <p>For example, I have the following URL: <strong>localhost/statistics/api_based</strong>
  When navigate, $query results are displayed, links are generated, OK.
  But, when I navigate to <strong>page 2</strong> for example, the URL will become:
  <strong>localhost/statistics/dll_based/index/2</strong> and <strong>page 3</strong> will become <strong>localhost/statistics/api_based/index/3</strong> , so therefore I am using segments.
  Now the problem is the query that is generated:</p>
  
  <pre><code>SELECT * FROM `shield_api` ORDER BY `id` ASC limit 20 offset 2
  </code></pre>
  
  <p><strong>offset 2</strong> - is the page number 2, and it should be 20, if I am correct.
  I am displaying 20 results per page, so first page is correctly showing results from 1 - 20, but then page 2 will display results from 2 - 21, so you get my point, it`s not OK...</p>
  
  <p>Here is my controller:</p>
  
  <pre><code>function index($offset = 0) {
          // Enable SSL?
          maintain_ssl ( $this-&gt;config-&gt;item ( "ssl_enabled" ) );
  
          // Redirect unauthenticated users to signin page
          if (! $this-&gt;authentication-&gt;is_signed_in ()) {
              redirect ( 'account/sign_in/?continue=' . urlencode ( base_url () . 'statistics/api_based' ) );
          }
  
          if ($this-&gt;authentication-&gt;is_signed_in ()) {
              $data ['account'] = $this-&gt;account_model-&gt;get_by_id ( $this-&gt;session-&gt;userdata ( 'account_id' ) );
          }
  
          $per_page = 20;
          $qry = "SELECT * FROM `shield_api` ORDER BY `id` ASC";
  
          //$offset = ($this-&gt;uri-&gt;segment ( 4 ) != '' ? $this-&gt;uri-&gt;segment ( 4 ) : 0);
  
          $config ['total_rows'] = $this-&gt;db-&gt;query ( $qry )-&gt;num_rows ();
          $config ['per_page'] = $per_page;
          $config ['uri_segment'] = 4;
          $config ['base_url'] = base_url () . '/statistics/api_based/index';
          $config ['use_page_numbers'] = TRUE;
          $config ['page_query_string'] = FALSE;
          $config ['full_tag_open'] = '&lt;ul class="pagination"&gt;';
          $config ['full_tag_close'] = '&lt;/ul&gt;';
          $config ['prev_link'] = '&amp;laquo;';
          $config ['prev_tag_open'] = '&lt;li&gt;';
          $config ['prev_tag_close'] = '&lt;/li&gt;';
          $config ['next_link'] = '&amp;raquo;';
          $config ['next_tag_open'] = '&lt;li&gt;';
          $config ['next_tag_close'] = '&lt;/li&gt;';
          $config ['cur_tag_open'] = '&lt;li class="active"&gt;&lt;a href="#"&gt;';
          $config ['cur_tag_close'] = '&lt;/a&gt;&lt;/li&gt;';
          $config ['num_tag_open'] = '&lt;li&gt;';
          $config ['num_tag_close'] = '&lt;/li&gt;';
          $config ["num_links"] = round ( $config ["total_rows"] / $config ["per_page"] );
  
          $this-&gt;pagination-&gt;initialize ( $config );
  
          $data ['pagination_links'] = $this-&gt;pagination-&gt;create_links ();
  
          //$data ['per_page'] = $this-&gt;uri-&gt;segment ( 4 );
  
          $data ['offset'] = $offset;
  
          $qry .= " limit {$per_page} offset {$offset} ";
  
          if ($data ['pagination_links'] != '') {
              $data ['pagermessage'] = 'Showing ' . ((($this-&gt;pagination-&gt;cur_page - 1) * $this-&gt;pagination-&gt;per_page) + 1) . ' to ' . ($this-&gt;pagination-&gt;cur_page * $this-&gt;pagination-&gt;per_page) . ' results, of ' . $this-&gt;pagination-&gt;total_rows;
          }
  
          $data ['result'] = $this-&gt;db-&gt;query ( $qry )-&gt;result_array ();
  
          $this-&gt;load-&gt;view ( 'statistics/api_based', isset ( $data ) ? $data : NULL );
      }
  </code></pre>
  
  <p>Can someone point me to my problem ? Any help is apreciated.</p>