Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Show paginated results on your website

Wednesday, November 4th, 2015 by Servage

paginationPagination means dividing larger result sets into multiple pages instead of having one very long list on a single page. It is used widely to make web pages more comprehensible and to allow better load times. Pagination improves performance for such large result sets both on the server back end and client front end. The database needs to load less results, the server needs to transmit less data via the HTTP response, and the client browser needs to handle and render less HTML. Therefore pagination is an overall improvement to large lists, and should be integrated from the beginning for any type of data that could potentially span over many rows.

How pagination works

Pagination is usually implemented by adding a “page” parameter to the URL. You could also make the amount of items per page a dynamic parameter, but usually this is a setting on the server side. If used dynamically, you should at least implement a sanity check for the number to be within a given acceptable range. Otherwise users with a malicious intent could abuse this parameter, and thereby create an overload on the server.

Image an example URL like “http://domain.com/products/list” would show a list of 10 products per page. Now, with a page parameter this could be either “http://domain.com/products/list/3″ or “http://domain.com/products/list?page=3″ depending on your desire to use SEO friendly URLs or not.

The page number basically indicates to the server which offset should be used when loading entries from the database. If you have configured it to show 10 products per page, then page three indicates that it should show products from 20-30. In MySQL this is achieved by using “LIMIT 20, 10″ (remember that result counts start at 0 and not 1).

Sort key and order

Pagination becomes really powerful in combination with sorting. Imagine your products have a price and the user should be allowed to sort the list by that column, either ascending or descending. The URL could then look like “http://domain.com/products/list?page=3&sort_by=price&sort_order=ascending”, which would append an “ORDER BY price ASC” to the MySQL select statement.

Page links and result count

On your website you probably want to give the user links to navigate between the pages. For this purpose you can create next/previous links (by adding/removing a count from the current page number, unless you are at the first/last page) and links to each page (by counting the amount of pages, and creating a link for each page, like the one you know from e.g.  Google search results). For this purpose you need the amount of results, which can be received by adding a counter statement with MySQL in addition to your select statement. By knowing the amount of results and the configured products per page, you can compute all the pagination links.

The methods stated above are relatively simple to implement, and actually most frameworks already have components for this included. In a future article it will be described how to implement pagination for an actual project using the Laravel framework.

 

Show paginated results on your website, 4.2 out of 5 based on 6 ratings
Categories: Guides & Tutorials

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.