This is a very easy to install pagination class that takes a PHP array and parses it into an array.
To install into one of your personal array's start by including the main file and creating the pagination object.
<?php
include 'pagination.class.php';
$pagination = new pagination;
?>
Once this is done, simply do the following with your own array.
For this tutorial i will create some test data, in multidimensional array, this is not essential - it's only for an example.
<?php
$products[] = array(
'Product' => 'Product 1',
'Price' => rand(100, 1000),
);
$products[] = array(
'Product' => 'Product 2',
'Price' => rand(100, 1000),
);
?>
Now i have my test data, i need to parse the data through the pagination class so that i can extract the part of the array that i need for the current page i am on, like so. I identify the array using the array's name and then i specify how many results per page i would like, for this example i have selected 20.
<?php
$productPages = $pagination->generate($products, 20);
?>
Now that i have the results for the current page ready i simply loop through them and output the results.
<?php
foreach ($productPages as $productArray) {
// Show the information about the item
echo '<p><b>'.$productArray['Product'].'</b> 243'.$productArray['Price'].'</p>';
}
?>
Now i have the results partitioned into the sections i have chosen i now need to output the page numbers, this can be done like so:
<?php
echo $pagination->links();
?>
And that's it, for ease of use the copy and pastable code is below, you will need to download the pagination class and this can be done using the download link at the top of the page.
<?php
// Include the pagination class
include 'pagination.class.php';
// Create the pagination object
$pagination = new pagination;
// some example data
foreach (range(1, 100) as $value) {
$products[] = array(
'Product' => 'Product '.$value,
'Price' => rand(100, 1000),
);
}
// If we have an array with items
if (count($products)) {
// Parse through the pagination class
$productPages = $pagination->generate($products, 20);
// If we have items
if (count($productPages) != 0) {
// Create the page numbers
echo $pageNumbers = '<div>'.$pagination->links().'</div>';
// Loop through all the items in the array
foreach ($productPages as $productID => $productArray) {
// Show the information about the item
echo '<p><b>'.$productArray['Product'].'</b> 243'.$productArray['Price'].'</p>';
}
// print out the page numbers beneath the results
echo $pageNumbers;
}
}
?>
Thanks for reading.

February 28th, 2008 at 5:31 pm
Very use for beginners!
I've often wondered how people can use 500 lines for a pagination script - although they're never the shortest bits of coding.
March 24th, 2008 at 2:45 am
[...] Smarty Pagination With the combined effort of my PHP Array Pagination script and this tutorial you will be able to pagination results (database & non database driven [...]
March 26th, 2008 at 4:22 pm
How would you go about adapting this to work with a mysql database.
March 26th, 2008 at 10:58 pm
@Adam
Pretty simple really, just add the result array into the pagination, like so:
Then parse it into the pagination object like so:
I hope that helps, it's pretty simple really ... !
April 28th, 2008 at 6:13 am
This is a very useful script for paging with the class !!
Greate Help. Thanks.
May 16th, 2008 at 8:43 am
Very nice man! i`ll try it.
June 15th, 2008 at 12:10 pm
is there a way to feed XML data to the pagination?... if so... can you give me the code please... i'm a real newbie
November 11th, 2008 at 4:48 am
Just wondering if this topic is still alive and if I can get some help with a script I wish to paginate
Regards
Dave
November 12th, 2008 at 11:40 pm
Heya, I will explain this the best way I can, the 1st lot of code below is my pagination for my members list, its done with mysql, now I have a chat site and would like to paginate the roomlist as well, but it can only be done without mysql, I know this can be done but im not sure on how, ive added the roomlist code below the pagination code to show u how the roomlist grabs and echos from the server, someone said yes its easy, just do it thru arrays, but again I dont have much of an idea on how to, so if anyone can help me out it would be much appreciated..
<?php
include('config.php');
include('user_check.php');
// If current page number, use it
// if not, set one!
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
// Define the number of results per page
$max_results = 10;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
// Perform MySQL query on only the current page number's results
$userselect = mysql_query("SELECT * FROM users LIMIT $from, $max_results");
$num = mysql_num_rows($userselect);
print("
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM users"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "Select a Page";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "Previous ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "$i ";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "Next";
}
echo "";
?>
roomlist below
November 12th, 2008 at 11:42 pm
roomlist
January 6th, 2009 at 12:48 pm
Hi,
I have the paginated data with me. I want to provide a "Select All" link on click
of which I can select all the paginated items on all the pages. Can anyone help me
with this?
January 9th, 2009 at 9:16 pm
I was wondering if anyone had implimented pagination in groups of 5.
ie First<<PreviousNext>>Last.
My recordset returns 600 pages, and I would like to group them.
Thanks
Tim
January 19th, 2009 at 10:21 am
Excellente!!!
January 19th, 2009 at 11:05 am
Couple of bugs?
Bug One: $this->implodeBy has no value.
Solution: Should be whitespace ' '.
Bug Two: an assumption is made that there are GET parameters. If there are none, the code breaks.
Solution: In links() set the queryURL to blank outside of the GET if condition:
$queryURL = '';
// Concatenate the get variables to add to the page numbering string
if (count($_GET)) {
Otherwise, this was great. Just what I was looking for. Thanks!
January 20th, 2009 at 10:01 am
Good Article
March 20th, 2009 at 10:24 am
Really nice array pagination class. Love it, I am using this for one of my small project. Thanks a lot.
April 27th, 2009 at 6:39 am
Hey All,
Very good article - but I am having an issue where my results are filtered by people's advanced search criteria (Suburb) (Sale or Lease) (Price). But when they select other pages it seems to forget there search criteria and just display all results. I use $_POST for my search criteria.
Any fixes/ideas?
Cheers,
George
April 29th, 2009 at 10:23 am
Anyone knows how to adapot the script to let the visitors choose how many entries per page they want displayed ? 5, or 10 or 20, etc ....
Thanks
June 5th, 2009 at 7:02 pm
Hey,
How to display just 10 pages (like in pagination from google) if I have more then 10 pages
Something like that: <> and if I click next :<>
I hope you understand
Can somebody help me?
June 7th, 2009 at 10:21 am
Can I use two paging instances on the same page?
June 19th, 2009 at 12:14 am
Say if I wanted a maximum of 3 records to be displayed per link I would change the number in this line:
$productPages = $pagination->generate($products, 3);
June 19th, 2009 at 9:24 pm
Thank you for the code. I have it functioning as I need, however, how can I add something like:
showing items 1-10 of 20
June 20th, 2009 at 1:38 am
Great script. I do find if length of the items in the array is less than 5 the scripts does seems to work...any ideas?
October 6th, 2009 at 11:25 pm
Hi, first of all, great script, i hade some issues implementing it but now works great, the only thing i could not fix was..
The navigation (where all the pages are), how can i limit the amount of pages it displays.. for example i just want 10 pages tops.
<< First Last >>
but if i have more results than the 10 pages can contain, i get more than 10 pages and thus, the navigation can grow endless.
<< First Last >>
Is there a way to hide the numbers from 10+ and while moving say to 11 display something like
<< First Last >>
I'm not a php wiz so i'm having some trouble with that, plus your code is really advanced lol.
October 22nd, 2009 at 3:04 pm
i used this on http://theweightoflight.com/photos/today/, a page i use to upload snapshots from my phone. it's the easiest and most flexible pagination script i could find. everything else has so many bells and whistles. i'm not that skilled with php and i was able to follow your logic and alter it a bit for my needs within an hour or so. thanks for sharing!