WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : PHP5 Paging Object Using Singleton Pattern
Categories : PHP, PHP Classes Click here to Update Your Picture
Joseph Crawford
Date : Aug 31st 2004
Grade : 2 of 5 (graded 5 times)
Viewed : 6427
File : 4005.zip
Images : No Images for this code example.
Search : More code by Joseph Crawford
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

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


if you have any questions about this code you can feel free to leave comments or to email me using weberdev@codebowl.com



very simple ftp class
Categories : PHP, PHP Classes, FTP
PHP Paypal IPN Integration Class v1.0.0
Categories : PHP, PHP Classes, Payment Gateways
A Timing Class
Categories : PHP, PHP Classes, Date Time
The class to check load time of your script VERY usefull for relatively slow applications, but not only..
Categories : PHP, PHP Classes, Debugging
Create HTML forms dynamicly using Javascript & PHP
Categories : PHP, PHP Classes, Java Script
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
RSS parser. Parses RSS into an array. Quick and nasty but does the job. No checking is done for correct Tags, only correct XML. PHP4 needed to display result (uses print_r).
Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS)
These PHP Classes Check if a host is alive using various methods.
Categories : PHP, PHP Classes, Sockets, CURL
an example of the cyberlib payment class
Categories : PHP, PHP Classes, Ecommerce, Credit Cards
Power Form Validation
Categories : PHP, PHP Classes, Data Validation
MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes
pcCalendar class - Allows for the creation of calendars in HTML pages. All output functions can be easily overridden, refer to article 1471 for an example.
Categories : PHP, Date Time, Calendar, PHP Classes
Class for sending mail with MIME attachments in multipart format using external sendmail, mimencode and zip
Categories : Email, Network, PHP, PHP Classes
A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function.
Categories : PHP, PHP Classes, Calendar, Date Time
Browser Detecor Class
Categories : PHP Classes, PHP, HTML
 Joseph Crawford wrote :1257
i have also converted this to php 4 if anyone wants it let me know and i can post it :D