PHP
  Home arrow PHP arrow Page 2 - Previous or Next? Paginating Records with PHP - Part 3
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Previous or Next? Paginating Records with PHP - Part 3
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 14
    2005-05-10


    Table of Contents:
  • Previous or Next? Paginating Records with PHP - Part 3
  • Object-oriented paging: anatomy of the "Pager" class
  • Controlling the look of database records: defining a template file
  • Going deeper: a detailed coverage at the "displayRecords()" method
  • The final round: completing the "displayRecords()" method

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Previous or Next? Paginating Records with PHP - Part 3 - Object-oriented paging: anatomy of the "Pager" class
    ( Page 2 of 5 )

    In order to set up the blueprints for the "Pager" class, let's start by defining the methods that compose it. As we saw in the previous article, the class will display two methods to work: the constructor itself and the "displayRecords()" method, which will take care of creating both the visual output for database records, and the corresponding paging links. Here's the first look at the class structure:

    class Pager {

    // properties declaration

    function Pager(){

    // parameters initialization

    }

    function displayRecords(){

    // code to display records and paging links

    }

    }

    As you can see, the skeleton of the class is very simple, and extremely understandable. So, let's examine the constructor to get a detailed understanding of its logic. At an initial stage, this method will accept only three incoming parameters: a reference to a MySQL's connection identifier, to provide database connectivity inside the class; an SQL query to get a result set (specifically a SELECT statement); and finally, the number of records per page to be displayed at a time.

    Is that really all there is to it? Just three parameters and we're in business with the class constructor? Well, we'll add a couple of arguments later, but right now, the class structure might be redefined, including parameter initialization, like this:

    class Pager {

    var $conId; // connection identifier

    var $query; // query performed

    var $numRecs; // number of records per page

    function Pager(&$conId,$query,$numRecs=10){

    // validate connection Id

    (mysql_query('SELECT 1',&$conId))?$this->conId=$conId:die('Invalid connection identifier.');

    // validate query

    (preg_match("/^SELECT/",$query))?$this->query=$query:die('Invalid query '.$query);

    // validate number of records per page

    (is_int($numRecs)&&$numRecs>0)?$this->numRecs=$numRecs:die('Invalid number of records '.$numRecs);

    $this->output='';

    }

    function displayRecords(){

    // code to display records and paging links

    }

    }

    Now things are getting a bit more complex -- but not too complex, really. Let's take a look at what the constructor does. Since it accepts the three mentioned parameters, before assigning them as class properties, they should be properly validated.

    In order to do that, first the methods check whether the connection identifier $conId corresponds to a valid MySQL connection, performing a simple "SELECT 1" statement against the selected database. If the query is successful, we have a valid MySQL connection identifier. Indeed, this little trick is useful to validate this kind of connection parameter. 

    Please notice that we're using a reference to the connection identifier, not a copy; this is the reason for the usage of the (&) ampersand operator, preceding the parameter. 

    The next task that the constructor performs is to validate the incoming SQL query, by checking whether it starts up with the string "SELECT". Since we don't need another statement, any query that doesn't fit this format is considered invalid, killing the script with a call to "die()".

    The final parameter to be validated is the number of records per page, $numRecs, which must be a positive integer. In this case, I've opted to specify a default value of 10 records to be shown at a time. As usual, if it happens that this argument is not valid, the class is simply killed. 

    By this point, the method has nicely validated the three parameters, assigning them as class properties. However, remember I said that we would add two more incoming parameters? Right, we need to provide the class a flexible mechanism to control the look and feel of database records, as well as of the paging links. Doing so, we'll make it more portable and extensible. How can it be done? We will simply implement a template file for records and a style ID for links. If this sounds confusing to you, just keep reading to learn more.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT