preload
Thumbnails Generator Class Sticky Caps
Feb 27

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.

25 Responses to “PHP Array Pagination”

  1. Adam Says:

    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.

  2. Smarty Pagination . Lots of Code Says:

    [...] 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 [...]

  3. Adam Says:

    How would you go about adapting this to work with a mysql database.

  4. lotsofcode Says:

    @Adam

    Pretty simple really, just add the result array into the pagination, like so:

    <?
    $dataCollection = array();
    $getData = mysql_query('SELECT * FROM tablename');
    if ($rowData = mysql_fetch_assoc($getData)) {
    do {
    $dataCollection[] = $rowData;
    } while($rowData = mysql_fetch_assoc($getData));
    }
    ?>

    Then parse it into the pagination object like so:

    // Include the pagination class
    include 'pagination.class.php';
    // Create the pagination object
    $pagination = new pagination;
    // parse the data
    $dataPages = $pagination->generate($rowData, 20);

    foreach ($dataPages as $dataKey => $dataArray) {
    // Show the information about the item
    echo '<p><b>'.$dataArray['fieldname'].'</b> '.$dataArray['fieldname2'].'</p>';
    }

    I hope that helps, it's pretty simple really ... !

  5. Payal Says:

    This is a very useful script for paging with the class !!
    Greate Help. Thanks.

  6. AckerMANn Says:

    Very nice man! i`ll try it.

  7. Joakim Says:

    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

  8. Dave Says:

    Just wondering if this topic is still alive and if I can get some help with a script I wish to paginate

    Regards

    Dave

  9. Dave Says:

    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

  10. Dave Says:

    roomlist

  11. Supriya Says:

    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?

  12. Tim Says:

    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

  13. c Says:

    Excellente!!!

  14. c Says:

    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! :)

  15. Sachin Says:

    Good Article

  16. Irenelim Says:

    Really nice array pagination class. Love it, I am using this for one of my small project. Thanks a lot.

  17. George Says:

    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

  18. Peter Says:

    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

  19. Virgil Says:

    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?

  20. sharmily Says:

    Can I use two paging instances on the same page?

  21. Alonzo Says:

    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);

  22. Maulsmash Says:

    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

  23. maulsmash Says:

    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?

  24. neocorps Says:

    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.

  25. miles Says:

    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!

Leave a Reply

 
 
Please enter the word above into the box below, the word is not case sensitive.
 
If you have trouble reading the word above then click here to load a new word.
Security word: