Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As David Graham says, you're missing your LIMIT and OFFSET. The CodeIgniter pagination is fairly basic, since the underlying "ActiveRecord" is itself very basic (in essence, a query builder, NOT an ORM).</p> <p>So, you need to think on the SQL level, how do you get "pages" of results that you want.</p> <p>Below I am including sample code from one of my projects.</p> <p><strong>CONTROLLER:</strong></p> <pre><code> // set up pagination $itemsPerPage = 20; $totalItems = $this-&gt;Location_model-&gt;count_locations( $locationName, $locationType, $locationParent ); // get itemStartIndex from URL if specified, otherwise default to 0 if ( $this-&gt;uri-&gt;segment(3) ) { $itemStartIndex = $this-&gt;uri-&gt;segment(3); } else { $itemStartIndex = '0'; } $this-&gt;load-&gt;library('pagination'); $config['base_url'] = site_url('admin/locations'); $config['total_rows'] = $totalItems; $config['per_page'] = $itemsPerPage; $config['uri_segment'] = 3; // store offset in case user triggers a function and we want to come back to same page $this-&gt;session-&gt;set_flashdata('pagination_offset', $itemStartIndex); $this-&gt;pagination-&gt;initialize($config); $data['pagination'] = $this-&gt;pagination-&gt;create_links(); // get current locations from database $records = $this-&gt;Location_model-&gt;get_locations( $locationName, $locationType, $locationParent, $itemStartIndex, $itemsPerPage ); ... // now $records gets passed to view for display </code></pre> <p><strong>MODEL:</strong></p> <pre><code>function count_locations($locationName = '', $locationType = '', $locationParent = '') { $this-&gt;db-&gt;select('id'); if ( $locationName != '' ) { $this-&gt;db-&gt;like('name', $locationName); } if ( $locationType != '') { $this-&gt;db-&gt;where('location_type', $locationType); } if ( $locationParent != '') { $this-&gt;db-&gt;where('parent_id', $locationParent); } $query = $this-&gt;db-&gt;get('locations'); return $query-&gt;num_rows(); } function get_locations($locationName = '', $locationType = '', $locationParent = '', $startIndex = '0', $limit = '') { $this-&gt;db-&gt;select('*'); if ( $locationName != '' ) { $this-&gt;db-&gt;like('name', $locationName); } if ( $locationType != '') { $this-&gt;db-&gt;where('location_type', $locationType); } if ( $locationParent != '') { $this-&gt;db-&gt;where('parent_id', $locationParent); } $this-&gt;db-&gt;order_by('name', 'asc'); $query = $this-&gt;db-&gt;get('locations', $limit, $startIndex); if ($query-&gt;num_rows() &gt; 0) { return $query; } else { return FALSE; } } </code></pre> <p>Notice that my method to do the query has the optional parameters $startIndex and $limit. These are MANDATORY for paging to work. The URI segment (in my case, segment 3) is going to directly tell your controller what OFFSET to use, and the <code>per_page</code> that you specify translates to the LIMIT (which in my case I use $itemsPerPage to store since it's referenced multiple times - actually I use a global value in my case but I hard coded it here for easier reference).</p> <p>Note you will also need a count method. You COULD just use your same query, load ALL results, and count it in the controller, but I like to use a separate count method that only generates a minimal query using my WHERE restrictions and selecting just COUNT('id'), which speeds it up considerably. (the downside of course is it's an anti-pattern to DRY since the same query code must be defined twice, once for your count method and once for the data querying method)</p> <p><strong>NOTE ON YOUR CODE</strong></p> <p>Instead of specifying those or_where statements in the controller, I would put them in the model, under a method that describes your search. This sort of search logic should definitely be in the model - the controller should only be accepting the routing, passing the inputs to the model, and then setting up the pagination and view with the results.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload