Pagination hitting the database twice !

code igniter
Print pagePDF pageEmail page

Pagination requires reading a record set twice:

  1. Once to read the whole set so that it can count the total number records
  2. Then to read a window of records to display

Here’s my way of doing it:

  • Using a public class property to store the total records (public $total_records)
  • Using a private function to build the query (that is common for both activities). The parameter ($isCount) we pass to this function reduces the amount of data the query generate, because for the row count we only need one field but when we read the data window we need all required fields.
  • The get_list() function first calls the database to find the total and stores it in $total_records and then reads a data window to return to the caller.
  • Remember we cannot access $total_records without first calling the get_list() method !

And now from the controller we call:




Leave a Reply

Your email address will not be published. Required fields are marked *