#native_company# #native_desc#
#native_cta#

Pagination of Results on a Web Page

By Eli White III and Jonathan Eisenhamer
on October 4, 2006

When creating browse or search functionality on a website, you many find that you end up with more data than the page can easily display at once. In these situations it makes for a better user experience to give users only a certain number of results and then present them with options to see the next and/or previous pages.
Listing 9.5.1 is an example of this that assumes that you have all your data in an array in the first place and just want to display parts of it at a time, reloading the page to view another set of options. This example needs customization to work properly for your own situation because all cases where pagination needs to happen end up slightly different.
Listing 9.5.1 Result Pagination
[ code listing ]
Two PHP functions make this task straightforward.The first function is http_build_ query(), which takes an array and automatically turns it into a GET query string for you. It takes care of all encoding issues and ensures a valid string.This makes it easy in our example to maintain the current GET parameters and just modify or add a few.
The second is array_chunk(), which breaks an array into equal-sized portions for you. This makes it easy when you have your entire dataset in memory to grab one page’s worth of it.
It is usually advantageous to not have all your data stored into memory at once when you are only going to display a subset of it. Not only will this save memory, but it also can speed up the reading of the results as well. How exactly to do this, however, depends on how your data is stored and retrieved, and sometimes is not possible.

This article is taken from the chapter titled, “Web Page Creation/XHTML/CSS”, which is excerpted from the new book, PHP 5 in Practice, authored by Eli White III and Jonathan Eisenhamer. Copyright 2007 by Sams Publishing. ISBN 0672328887. Reprinted with permission by Pearson Education; All rights reserved. A complete Table of Contents is available online.