lots of code take some code, share some code!

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.

7 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

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: