WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 : 11214
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

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

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



A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP, PHP Classes, Databases, MySQL, HTML and PHP
PHP class generator, must be used from Command line interface.
Categories : PHP, PHP Classes, Shell Scripting
XPertMailer - Sends TRUE Mails
Categories : PHP, Mail, SMTP, PHP Classes
My Box - PHP Class that calculates the volumetric weight of a package.
Categories : PHP, Object Oriented, PHP Classes, Beginner Guides, Payment Gateways
ECHO-PHP Class Real Time Transaction Processor v1.4.4 for Credit Cards and Checks / ACH
Categories : PHP Classes, Cybercash, Classes and Objects, Ecommerce, PHP
ElfReader: An ELF (Executable and Linking Format) header information in PHP. Shows how to use the UNPACK function to read data.
Categories : PHP, Linux, PHP Classes
Parsing Simple Template Files and Data
Categories : PHP, PHP Classes, Templates, Regexps
Scan Apache access log files and report possible worms attack
Categories : PHP, PHP Classes, Security, Apache, Log Files
PHP CLASS for ORACLE (database connectivity)
Categories : PHP, PHP Classes, Classes and Objects, Databases, Oracle
Export Excel Dynamically to Csv then to mysql
Categories : PHP Classes, Excel, PHP
IRC Client class (RFC1459 Compliant)
Categories : PHP, PHP Classes, IRC
Simple database class
Categories : PHP, PHP Classes, MySQL, Databases
PHP interface class to the eBusiness Charts generatation remote service.
Categories : PHP, PHP Classes, Graphics, Charts and Graphs
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
A time measuring and performance benchmarking class
Categories : PHP, PHP Classes, Testing, Debugging, Date Time
 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