Your IP: 38.107.179.218 

PHP5 Paging Object Using Singleton Pattern

Posted by: Asif D. Khalyani

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.

data.inc
<?php
$data
= array(
   
0 => array(
       
'isbn' => '0764549634',
       
'title' => 'MySQL/PHP Database Applications'
   
),
   
1 => array(
       
'isbn' => '0672326205',
       
'title' => 'PHP, MySQL and Apache'
   
),
   
2 => array(
       
'isbn' => '0672317842',
       
'title' => 'PHP And MySQL Web Development'
   
),
   
3 => array(
       
'isbn' => '0130895725',
       
'title' => 'C How To Program'
   
),
   
4 => array(
       
'isbn' => '159200153',
       
'title' => 'PHP Game Programming'
   
),
   
5 => array(
       
'isbn' => '0130463469',
       
'title' => 'Core PHP Programming'
   
),
   
6 => array(
       
'isbn' => '076454716',
       
'title' => 'PHP 4 Bible'
   
),
   
7 => array(
       
'isbn' => '1893115518',
       
'title' => 'Beginning PHP 5 and MySQL'
   
),
   
8 => array(
       
'isbn' => '0672325616',
       
'title' => 'Advanced PHP Programming'
   
),
   
9 => array(
       
'isbn' => '0596005601',
       
'title' => 'Learning PHP5'
   
),
   
10 => array(
       
'isbn' => '1565926811',
       
'title' => 'PHP Cookbook'
   
),
   
11 => array(
       
'isbn' => '0596006365',
       
'title' => 'Upgrading To PHP5'
   
),
   
12 => array(
       
'isbn' => '0764557467',
       
'title' => 'PHP5 and MySQL Bible '
   
),
   
13 => array(
       
'isbn' => '1931841349',
       
'title' => 'PHP Essentials'
   
),
   
14 => array(
       
'isbn' => '0764557440',
       
'title' => 'Beginning PHP, Apache, MySQL Web Development'
   
),
   
15 => array(
       
'isbn' => '0130889032',
       
'title' => 'Essential PHP for Web Professionals'
   
)
);
?>



this is the front end for the class object

index.php
<?php
include_once('include/class/Pager.class.php');

// 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

$link = mysql_connect('localhost', 'user', 'password');
mysql_select_db('nyaspdp_org', $link);
$query = mysql_query("SELECT * FROM courses");

// 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>";
?>



this is the heart of the script

include/class/Pager.class.php
<?php
/*****************************************************
**
** Pager Class
**
** This class will take a 2-dimensional array and it will
** create a page based array based on the value of $perpage
**
** @author Joseph Crawford Jr. <jcrawford@codebowl.com
** @copyright © 2004, Codebowl Solutions
** @example /examples/Pager/index.php
** @license http://opensource.org/licenses/gpl-license.php GNU Public License
**
**
*****************************************************/
Class Pager {

    static private
$instance = false;

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

Back to Index Page