PHP
  Home arrow PHP arrow Page 4 - 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 - Defining the "displayRecords()" method
    ( Page 4 of 6 )

    The "displayRecords()" method is the workhorse of the class, and accepts one parameter, $page, which determines which page of the retrieved records we want to see. So, let's start by defining its signature and then explain how it works:

    function displayRecords($page){

    // calculate number of pages

    $numPages=ceil(count($this->totalRecs)/$this->numRecs);

    // validate page pointer

    if(!preg_match("/^\d{1,2}$/",$page)
    ||$page<1||$page>$numPages){

    $page=1;

    }

    // retrieve corresponding records from flat file

    $records=array_slice($this->totalRecs,($page-1)*$this-
    >numRecs,$this->numRecs);

    foreach($records as $row){

    $columns=explode('|',$row);

    foreach($columns as $column){

    $this->output.=$column.'&nbsp;';

    }

    $this->output.='<br />';

    }

    // create previous link

    if($page>1){

    $this->output.='<a href="'.$_SERVER['PHP_SELF'].'?
    page='.($page-1).'">&lt;&lt;Previous</a>&nbsp;';

    }

    // create intermediate links

    for($i=1;$i<=$numPages;$i++){

    ($i!=$page)?$this->output.='<a href="'.$_SERVER
    ['PHP_SELF'].'?page='.$i.'">'.$i.'</a>&nbsp;':$this-
    >output.=$i.'&nbsp;';

    }

    // create next link

    if($page<$numPages){

    $this->output.='&nbsp;<a href="'.$_SERVER
    ['PHP_SELF'].'?page='.($page+1).'">Next&gt;&gt;</a> ';

    }

    // return final output

    return $this->output;

    }

    As I said previously, the method accepts the $page parameter, which is the page pointer to determine which page we wish to view. However, before any validation process takes place, it calculates the number of pages needed to display the complete number of records. This value is obtained with the following lines:

    // calculate number of pages

    $numPages=ceil(count($this->totalRecs)/$this->numRecs);

    Here we're dividing the total number of records read from the file by the number of records per page. Using ceil(), we round a decimal number up; in this way 3.25 becomes 4. Doing so, we get an integer value as the number of pages to be used.

    Having obtained the number of pages, the method is now capable of validating the $page parameter. Maybe you're asking… why should I check out the validity of this parameter? The answer is that we don't want this value to be tampered with by external code. We're using the same technique shown in the first article. It's really funny to see how many websites fail to validate page pointers, and end up modifying paging links according to code injected through the querystring, even if they're using a URL rewriter. We don't want this to happen, so the method checks if the $page pointer is valid, like this:

    if(!preg_match("/^\d{1,2}$/",$page)
    ||$page<1||$page>$numPages){

    $page=1;

    }

    Any attempt to tamper with the page pointer will result in a value of 1. Doing so, the method will display the first page. Simple and straightforward, right?

    Are you getting tired of long explanations and want to see the class in action? So am I! But there are a few lines of code that still need to be explained. Just be a bit patient and keep reading.



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