PHP
  Home arrow PHP arrow Page 3 - Previous or Next? Paginating Records with PHP - Part 2
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 2
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2005-05-03


    Table of Contents:
  • Previous or Next? Paginating Records with PHP - Part 2
  • Taking the procedural approach: the friendly "paginateRecords()" function
  • Taking the OOP approach: defining the "Pager" PHP class
  • Defining the "displayRecords()" method
  • Source code ahead: completing the "displayRecords()" method
  • Pager class implementation

  • 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 2 - Taking the OOP approach: defining the "Pager" PHP class
    ( Page 3 of 6 )

    There are still many heated discussions about the pros and cons of Procedural and Object Oriented Programming in PHP. Regardless of the different opinions voiced about the topics, generally developers seem to agree on one point: OOP is best suited for medium or larger projects, while a procedural approach works better for small applications. Nothing stops you from applying the method that best fits your particular needs. However, with an extremely versatile language such as PHP, as Web applications evolve from simple scripts to full-fledged programming structures, the OOP approach is definitely the right way to go.

    Keeping in mind the concepts explained above, we'll tackle the record paging issue, creating a PHP class to handle records in an efficient manner. Our "Pager" class will present only two methods: the corresponding class constructor and the "displayRecords()" method, which, not surprisingly, will display the records and create the paging links. Let's start by defining the blueprints for the class:

    class Pager {

    var $output;

    var $totalRecs;

    var $numRecs;

    function Pager($dataFile,$numRecs){

    // code for parameters setup

    }

    function displayRecords($page){

    // code to display records and paging links

    }

    }

    As you can see, the structure of the class is very simple, and displays three properties: $output, $totalRecs and $numRecs, respectively. The first property will store progressively the output generated, including record values and paging links. The second one represents the total  records retrieved from the data file, and finally the third argument shows the number of records per page being displayed. Indeed, the class structure is pretty self-explanatory.

    Once we've defined the skeleton of the class, it's time to add some functionality to the constructor. Let's take a look at its complete definition:

    function Pager($dataFile,$numRecs=5){

    // validate data file

    (file_exists($dataFile)&&count(file($dataFile))>0)?
    $this->totalRecs=array_reverse(file($dataFile)):die
    ('Data file not valid.');

    // validate number of records per page

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

    }

    As you can deduce from the code for the constructor, it accepts only two parameters: the data file and the number of records to be shown per page. In this particular case I've decided to give a default value of five records, but as usual, could be changed by passing another value to the method.

    Next, the constructor checks to see whether the data file is valid and  not empty. If the file is considered valid, then the constructor gets the complete records from the data file, assigning them as a class property. Otherwise, the class is stopped with a die() statement.

    What is the constructor's next task? It validates the number of records per page, by checking to see whether the value passed is a positive integer. If the condition is meet, the method assigns the value as another class property. If the value is not valid, the class is simply killed using a regular die() statement.

    Notice the simplistic nature of the constructor, which performs the validation of parameters and sets up the corresponding properties. Once the method has completed its tasks, we need to define the other method of the class: "displayRecords()". Let's take a deep breath and jump straight into its code definition.



     
     
    >>> 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 4 Hosted by Hostway
    Stay green...Green IT