PHP
  Home arrow PHP arrow Page 5 - Previous or Next? Paginating Records w...
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 3
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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 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. 


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this third part of the series, I've developed a simple yet powerful class to...
       · Good job!!!i have tried to play around with the class, modify here and there.BUt...
       · Thank you for the comments.Analyzing your situation, I suggest that you may...
       · i've found lots of examples using MySQL as the database, does anyone know how to do...
       · If you're using PEAR, take a look at the PEAR :: DB package. It offers good support...
       · Hello,I recently read this article and tried the code. It works great, thanks a...
       · Hello,Thanks for the comments and feedback. I really appreciate your time to...
       · Looks like this is just what I'm looking for. I'm a new-be to PHP and have a...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - 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
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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