Feb 27 2008
PHP Array Pagination
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