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.

Post Comments