PHP snippets, tutorials and scripts

PHP array pagination

Pagination in PHP is a topic covered by a lot of tutorials and is therefore quite saturated. Although I’m not going to introduce any wild new concepts into this tutorial I will explain how you can use pagination for data held within an array.

But why would you want to do that? Why would anyone want to provide pagination for data within an array in PHP? Normally you’d only need to paginate your data if you’ve got quite a lot of it in which case you’re most likely using some sort of database. With database systems of course pagination can be achieved relatively easily by specifying the offset parameters in an SQL query. But what if your data didn’t come from a database table and instead came from a flat file.

Take a look at the following code which shows exactly how it’s done.

// Data, normally from a flat file or some other source
$data = "Item 1|Item 2|Item 3|Item 4|Item 5|Item 6|Item 7|Item 8|Item 9|Item 10";

// Put our data into an array
$dataArray = explode('|', $data);

// Get the current page
$currentPage = trim($_REQUEST[page]);

// Pagination settings
$perPage = 3;
$numPages = ceil(count($dataArray) / $perPage);
if(!$currentPage || $currentPage > $numPages)
	$currentPage = 0;

$start = $currentPage * $perPage;
$end = ($currentPage * $perPage) + $perPage;

// Extract ones we need
foreach($dataArray AS $key => $val)
{
	if($key >= $start && $key < $end)
		$pagedData[] = $dataArray[$key];
}

Now for the explanation. To begin with I have created the $data array which contains a long line of items split up by the pipe '|' character. Realistically this would be real data however just for this tutorial I'll keep it simple. Then, using the explode() function I've cut up the $data variable into an array using '|' as the delimeter.

Line 8 simply gets the current page number if one is provided.

Lines 11 to 17 are all to do with the simple math calculations which make this array pagination work. Firstly set how many items we'd like to display per page into the variable, $perPage. In the example above I've set this to 3.

On line 12 we're working out how many pages there are going to be. This can be done by dividing the total number of items (by using the count() function) by the items per page value. Notice on this line that I'm also using the ceil() function. This basically rounds the number up (e.g. 5.134 becomes 6).

We then have a simply if statement on lines 13 and 14. It's basically saying that if no page number has been provided or if the provided page number is more than the number of pages, set it to 0. This stops people from trying to access pages which have no items.

On lines 19 and 20 we're setting the $start and $end variable which you might recognize if you've done pagination using SQL queries before. The $start variable holds the lowest ID of the item which can be displayed on this page. The $end variable is maximum cap which the items ID can be to be displayed (actually, it's one above, but this depends on how you do your if statement on line 22).

Great, we're nearly there. Now, on line 20 we start a foreach statement which loops through each of our data items. Inside this loop is a simple if statement to see if the id of the current data item is above (or equal to) the $start value and below the $end value. If it is then we place a copy of it into our $pagedData array.

Once the foreach loop has finished the $pagedData array now contains all of the data items which we should be displaying on the current page. All we have to do now is to loop through and display them. This has been shown in the following code snippet.

foreach($pagedData AS $item)
	echo $item . "
";

As far as displaying the data goes that's it. All we need to do now is to display the pagination links to let you navigate your way through the pages. Here's the code for that.

if($currentPage > 0 && $currentPage < $numPages)
	echo '« Previous page
'; if($numPages > $currentPage && ($currentPage + 1) < $numPages) echo 'Next page »
';

The above snippet consists of two quite simple if statements. The first is for displaying the "previous page" link and the second is for displaying the "next page" link.

Starting with the first, the if statement checking to see if the current page is more than 0 (you wouldn't have a previous link on the very first page) and if the current page is less than the total number of pages (to avoid displaying it on pages with no data).

The second if statement is checking to see if the total number of pages is more than the current page number (so you're not vieiwng the last page) and if there are any more pages after the current page.

Simple, right? That's all that you need to do to get simple PHP array pagination working. You should of course tweak the code provided here to fit your own purposes. Your data will most likely be more complex and you may want to display the items differently - now that you know how, it's all up to you.

September 16th, 2011