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.
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.
[...] 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 [...]
How would you go about adapting this to work with a mysql database.
@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 ... !
This is a very useful script for paging with the class !!
Greate Help. Thanks.
Very nice man! i`ll try it.
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
Just wondering if this topic is still alive and if I can get some help with a script I wish to paginate
Regards
Dave
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
roomlist
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?
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
Excellente!!!
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!
Good Article
Really nice array pagination class. Love it, I am using this for one of my small project. Thanks a lot.
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
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
Hello Peter,
Just add at top of this script:
$Per_Page = $_REQUEST['Num'];
& Change line:
$productPages = $pagination->generate($products, $Per_Page);
now i hope this script works for you also
Byee
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?
Can I use two paging instances on the same page?
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);
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
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?
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.
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!
Thanks for this php class.
With the fixes that "c" suggested, it works great.
Simple and easy
Thanks!
Hey There,
I have in my array about 500 products and I want to display pages 1 to 10 only and then like google if I click on 10 it shows me pages 10 through 19.
rather than having pages 1 to like 300.
Any ideas on how to implement this would be awesome!
Thanks in advance,
George
Very nice an useful script
Thank's
I'm trying to get the pagination script working properly but am failing. I have the example up and running but I cannot figure out how to make it work for my database. I saw your comment to "Adam" but still did not understand how to do it.
Could you please break it down for me? I couldn't figure out what your variables in your explanation was connected to. I really like the simplicity of your pagination and it using an array instead of directly pulling data from the database which is what I need done to implement it into my site search but first I need to properly figure out how it works.
So, if you're willing to help I can give an example. Say I want to pull the column "codes" from my database "posts" in my database and display it with pagination. Any help at all would be very much appreciated.
//First of all, you need to pull your data from your table
$result = mysql_query("SELECT codes FROM posts");
//Then iterate through all the items and assign them to a new array
while ($row = mysql_fetch_assoc($result)) {
$results[]=$row;
}
//Pass that array to the pagination class
$pages = $pagination->generate($results, 20);
//Then iterate through and show the field value
foreach ($pages as $pageArray) {
echo ''.$pageArray['codes'].'';
}
Working great.
Now I can use pagination without Javascript and don't have to fiddle around with it myself.
Thanks alot!
How would you go about adapting this to work with a mysql database.
This is very useful considering it can be adapted to any database or even within code. It's really helpful for me because I already have a class that interacts with databases and returns the result set as an array (if it's a "select").
I have modified the pagination class to mimic the google page numbers which is 10 numbers to the left, the current page, and 10 numbers to the right. Example page 1 would display (1 2 3 4 5 6 7 8 9 10 (11) 12 13 14 15 16 17 18 19 20 21). The current class takes a subset of the array based on $this->page. the value 21 is the length of the array you want.
The last line of the class is changed to:
if($this->page >= 1) {
$startValue = ($this->page - 11);
if ($startValue implodeBy, array_slice($links, $startValue, 21)).implode(' ', $slinks);
}
I tried to get this mod by nic to work but it does not any idea how to get this mod code to work?
I think somthing is a mis but I cant figure it out
The last line of the class is changed to:
$endvalue = 21;
if($this->page >= 1) {
$startValue = ($this->page - 11);
if ($startValue page implodeBy, array_slice($links, $startValue, $endvalue)). " " .implode(' ', $slinks);
}
some of the code appears broken like this line
$startValue page implodeBy
Anyone get this code fixed so that it works and show example of what this replaces?
Sorry for the repeted posts, the last post was cut off.
$endvalue = 21;
if($this->page >= 1) {
$startValue = ($this->page - 11);
if ($startValue page implodeBy, array_slice($links, $startValue, $endvalue)). " " .implode(' ', $slinks);
}
Great Job, its really cool & work nice... keep it up. : )
Thanks bro i solved my biggest pagination problem using this.
Again Thanks a lot.
Really cool stuff, thanks!
This is really great and was very helpful script indeed. Good work.