The following code consists of 3 files, i will show the code here however you can download the attachment for the code.
this is the file i have used to create a 2-dimensional array that holds the data to be paged. i have used PHP books for my array you could have used anything, *NOTE* i will also show how to do a database lookup and pass the results to the Pager object.
// include the data file if we use one
include_once('data.inc');
// check to see if the page is specified if so use that page otherwise use page 1
if(isset($_GET['p'])) $pg = $_GET['p'];
else $pg = 1;
// set the page to be used in the navigation links
$targetPage = $_SERVER['PHP_SELF'];
/*
THIS IS WHERE WE COULD DO A DATABASE QUERY
you could use mysql's functions or any other database abstraction
//setup the connection to the database and do the query
// set $data to be an empty array()
$data = array();
// create the 2-dimensional array since mysql_fetch_array only returns 1 record in an array
// we want to get all records and put them into a nice array.
// if you do a database query this is where you loop through all results and push the results into the $data array, mysql_fetch_array only returns one record in a single dimensional array, we need all result and a 2-dimensional array.
while($result = mysql_fetch_array($query)) {
// loop through the query results and add each result to the $data array
array_push($data, $result);
}
*/
// create the instance of the pager
// tell how many results per page, what page you are on
// the target page to use, and pass it the data
// the only thing required is $data but note you cannot leave
// out one of the middle parameters, but you can leave off the last 2
// the last one, or the last 3
$pager = Pager::instance($data, $pg, $targetPage, 15);
// call the getPage method and loop through each item that is contained on that page.
foreach($pager->getPage($pg) as $item) {
echo 'ISBN: '.$item['isbn'].'<br>';
echo 'Title: '.$item['title'].'<br><br>';
}
// this will print the navigation bar
echo "<div align='center'>".$pager->getNav()."</div>";
?>
// the property to use when properties that have not been defined
// have been set PHP uses the __set method when this happens.
private $properties;
// The array of data re-constructed into a format to display
// pages
public $pages;
// the constructor called when the object is created
private function __construct($data, $pageNum, $targetPage, $perpage) {
$this->perpage = $perpage;
$this->targetPage = $targetPage;
$this->pageNum = $pageNum;
$this->pages = array();
$this->build($data);
$this->buildNav();
}
// this is the function that checks to see if there is already
// an instance of this object, if there is it returns it
// otherwise it creates a new instance
static function instance($data=NULL, $pageNum=1, $targetPage=NULL, $perpage=10) {
if(!Pager::$instance) {
Pager::$instance = new Pager($data, $pageNum, $targetPage, $perpage);
}
return Pager::$instance;
}
// the magic __set method.
public function __set($name, $value) {
if(isset($name)) {
$this->properties[$name] = $value;
}
}
// the magic __get method.
public function __get($name) {
if(isset($this->properties[$name])) {
return $this->properties[$name];
} else {
return NULL;
}
}
// this function reconstructs the $data into a pagable array.
public function build($data=NULL) {
if (is_array($data)) {
$number_pages = count($data)/$this->perpage;
if (round($number_pages) < $number_pages) {
$number_pages = round($number_pages) + 1;
}
$k = 0;
for ($i = 0; $i < $number_pages; $i++) {
for ($j = 0; $j < $this->perpage; $j++) {
if (isset($data[$k])) {
$this->pages[$i][$j] = $data[$k];
}
$k++;
}
}
}
}
// this builds the navigation
public function buildNav() {
$this->nav = '';
foreach($this->pages as $key => $page) {
if($key == $this->pageNum-1) {
$this->nav .= ($key+1)." ";
} else {
$this->nav .= "<a href='".$this->targetPage."?p=".($key+1)."'>".($key+1)."</a> ";
}
}
}
// self explanatory
public function getNav() {
return $this->nav;
}
// self explanatory
public function getPage($num) {
return $this->pages[$num-1];
}
}
?>
if you have any questions about this code you can feel free to leave comments or to email me using weberdev@codebowl.com