PHP
  Home arrow PHP arrow Page 5 - 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 - The final round: completing the "displayRecords()" method
    ( Page 5 of 5 )

    Completing the definition of this method is matter of adding to it the capabilities to handle any template file for formatting purposes, and generating the paging links. Actually, we're not so far from the finish line. Let's add the necessary code to work with template files and the paging links. Here are the additional lines to complete the method:

    // previous method code goes here

    // read template file contents

    $templateContent=file($this->template);

    // add template header to final output

    $this->output=reset($templateContent);

    // move pointer to placeholders line

    $templateRow=next($templateContent);

    // replace placeholders

    while($row=mysql_fetch_row($result)){

    $tempOutput=$templateRow;

    for($i=0;$i<mysql_num_fields($result);$i++){

    $tempOutput=str_replace('{data'.$i.'}',$row
    [$i],$tempOutput);

    }

    // remove unpopulated placeholders

    $this->output.=preg_replace("/
    {data.}/",'',$tempOutput);

    }

    // add template footer to final output

    $this->output.=end($templateContent);

    // create paging links

    $this->output.='<div id="'.$this->linksId.'">';

    // 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> ';

    }

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

    // return generated output

    return $this->output;

    The majority of the listed code is fully commented and quite easy to understand. Let's begin by explaining how the method will format records using a template file. First, it reads the template file contents, storing them in the $templateContent array, like this:

    $templateContent=file($this->template);

    Please, remember that the template file has three lines, defined as a header section, a placeholders section and a footer section. In order to format the records, the method first adds the header section to the final output, resetting the array pointer, and retrieving the first line of the template file, in the following way:

    // add template header to final output

    $this->output=reset($templateContent);

    Now, it's time to move the pointers to the placeholders' line and replace them with the actual values:

    $templateRow=next($templateContent);

    // replace placeholders with actual data

    while($row=mysql_fetch_row($result)){

    $tempOutput=$templateRow;

    for($i=0;$i<mysql_num_fields($result);$i++){

    $tempOutput=str_replace('{data'.$i.'}',$row
    [$i],$tempOutput);

    }

    // remove unpopulated placeholders

    $this->output.=preg_replace("/
    {data.}/",'',$tempOutput);

    }

    After replacing the placeholders with the corresponding values, any unpopulated variables are removed from the final output, substituting the placeholders with an empty string, this way enhancing the class' flexibility. Finally, once records have been properly formatted, the footer line is appended to the output:

    // add template footer to final output

    $this->output.=end($templateContent);

    That efficiently completes the process of formatting the records. As you can see, the approach is flexible enough to change the overall visual appearance, by just changing simple template files.

    The rest of the code is nearly identical to the example listed in the second part, where the paging links are created by appending a <previous> link whenever possible, then generating the numbered links, and finally inserting a regular <next> link, when applicable. The only noticeable change hangs on providing an ID to tie a CSS style to the links, and offering a more polished appearance:

    // create paging links

    $this->output.='<div id="'.$this->linksId.'">';

    // 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> ';

    }

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

    // return generated output

    return $this->output;

    Having completed the definition for our "displayRecords()" class method, the class is already set up to be implemented on any existing or future Web application. Hopefully we've demonstrated that coding a PHP paging class is a not only an instructive experience, but also fun!

    Wrapping up

    That's about it for the moment. In the final part of this series, we'll run through several examples to show how the class can be implemented in real-world applications, providing a useful way to introduce paginated result sets in websites. In the meantime, feel free to play with the class code, adding your own improvements to it. 



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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