PHP
  Home arrow PHP arrow Page 3 - Previous or Next? Paginating Records w...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · This second article transits the OOP approach to build up a class to paginate data...
       · Thanks and congratulations for your articles...i was expecting this 2nd part... i...
       · Thanks a lot for the compliments about the articles. If you have found useful this...
       · Great! While waiting for the 3rd part, i'm going to 'train' php by modifying the...
       · Thanks for the comments. I'm glad you to develop your own paging solution. Just go...
       · Where you are paging by 10 records should that be... ...
       · We made the changes to the code.Thanks,Editorial Staff.
       · Yes, that was already pointed before as you can see in previous posts, and it was...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT