Jump To Content

LearnHub




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 so here.

<?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;
}
}
?>

Adapting this to work with a mysql database.

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>';
}


Thanks for reading.


  1. netsurge saidFri, 25 Jul 2008 05:22:28 -0000 ( Link )

    Where do you get the class? There is no download link at the top of the page

    Actions
    Vote
    Current Rating
    1
    Rate Up
    Rate Down
    1 Total Vote

    Post Comments

  2. laurellion saidWed, 30 Jul 2008 12:33:18 -0000 ( Link )

    So about that: download & unzip from here: http://www.filefactory.com/file/ece461/n/pagination_zip

    Actions
    Vote
    Current Rating
    1
    Rate Up
    Rate Down
    1 Total Vote

    Post Comments

  3. xavierhb saidTue, 13 Jan 2009 17:02:08 -0000 ( Link )

    I could’nt make work MySQL example… I would be very grateful If you could add more information about how to do it.

    Thanks and good job anyway!

    Actions
    Vote
    Current Rating
    0
    Rate Up
    Rate Down
    No Votes

    Post Comments

  4. ashishthakre saidThu, 09 Jul 2009 05:58:51 -0000 ( Link )

    how to change pagination dispaly templates in smarty for my website please tell me ihave Displaying 1-4 of 4 FAQs prev(image) Jump to Page dropdownbox(which show page link) of 1 next(image) Show textbox(for number of record per page) per page

    Actions
    Vote
    Current Rating
    1
    Rate Up
    Rate Down
    1 Total Vote

    Post Comments

  5. leetak saidTue, 25 Aug 2009 06:32:35 -0000 ( Link )

    How to get downlaod this class? please help me

    Actions
    Vote
    Current Rating
    1
    Rate Up
    Rate Down
    1 Total Vote

    Post Comments

Your Comment
Textile is Enabled (View Reference)