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.




Pagination function


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


Join the discussion

  1. Avatar
    Jinal says:


    I implemented this code and looks perfect but at one point i am stucked which is, my additional parameters are coming from filter code. which i am storing in $url variable. I am not getting perfect result with filtered value.

Leave a Reply

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