A bootstrap compatible pagination function in PHP for database queries

Below is the function that will generate a pagination markup as per the Bootstrap framework.

Demo

pagination

Usage

Pagination function

Summary

Let’s say that we have a page http://abc.com/users/all. Now instead of showing all the users on the same page we want to show only 10 users per page. So accordingly http://abc.com/users/all will show only first 10 users. Visiting http://abc.com/users/all?page=2 will show next 10 set of users and so on

To use the above pagination function we’ll need below values which are parameters required by our pagination function

  • Base url: $baseUrl will the url to which pagination function will append page number. For our current case it will be http://abc.com/users/all. So pagination function will append ?page=2 query string to it
  • Total results: $totalResults will be total number of result sets. Usually you would use count() function to fetch it. Let’s assume in our case we have 1000 users.
  • Results per page: $resultsPerPage will be number of records in each page
  • Current page: $currentPage will be current page being displayed. You would check $_GET['page'] for this value
  • Query string array: $queryStringArray Say you also want to append some search parameters to paginated links like name=Perials then you would set this to ['name'=>'Perials']. Every paginated link will have this in query string along with the page parameter. Eg http://abc.com/users/all?page=2&name=Perials

Fork on Github

https://github.com/perials/pagination-function

Leave a Reply

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