PHP
  Home arrow PHP arrow Page 2 - 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 procedural approach: the friendly "paginateRecords()" function
    ( Page 2 of 6 )

    In the first article, we defined a simple text file for storing information, where each line is considered a single record, and each column is a different field. In programming environments, this is known as a flat database. We went as far as setting up a rather basic function to paginate data retrieved from a text file. The function's source code was originally defined as shown below:

    function paginateRecords($dataFile,$page,$numRecs=5){

    $output='';

    // validate data file

    (file_exists($dataFile))?$data=array_reverse(file
    ($dataFile)):die('Data file not valid.');

    // validate number of records per page

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

    // calculate total of records

    $numPages=ceil(count($data)/$numRecs);

    // validate page pointer

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

    $page=1;

    }

    // retrieve records from flat file

    $data=array_slice($data,($page-1)*$numRecs,$numRecs);

    // append records to output

    foreach($data as $row){

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

    foreach($columns as $column){

    $output.=$column.'&nbsp;';

    }

    $output.='<br />';

    }

    // create previous link

    if($page>1){

    $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)?$output.='<a href="'.$_SERVER
    ['PHP_SELF'].'?
    page='.$i.'">'.$i.'</a>&nbsp;':$output.=$i.'&nbsp;';

    }

    // create next link

    if($page<$numPages){

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

    }

    // return final output

    return $output;

    }

    Although the function is basic, it's really flexible and portable, because it allows us to specify a data file to read from, and the number of records (in this case we're referencing file lines and not true database records) to be displayed at a time. Also, it's powerful enough to generate the classic <previous> - 1, 2, 3 …<next> navigational links. It's an elemental format, but with minor modifications, it might be changed to present a more polished look, just by tweaking the original code.

    One possible implementation for the function may be defined in the following example:

    <?php

    require_once('pager.php');

    $page=$_GET['page'];

    echo paginateRecords('data1.dat',$page);

    ?>

    In the above example, we've specified a data file "data1.dat" from which to read, and a default value of five records to be displayed at a time. Because of its flexibility, another case may be coded as follows:

    <?php

    require_once('pager.php');

    $page=$_GET['page'];

    echo paginateRecords('data2.dat',$page,10);

    ?>

    The previous case will display data from a "data2.dat" file and show ten records per page, reordering the paging links according to the new parameters. Definitely, we have to agree that our function is very versatile and extendable. Just play around with the code to make it fit your needs.

    While the function is handy for small projects, it lacks some of the complexities found in more sophisticated applications. Therefore, as I previously stated, we're going to develop a PHP class that will paginate records from a data file. It will keep the functionality of the initial procedural method, but offer the wide advantages inherent to Object Oriented Programming. So, let's start defining the PHP class.



     
     
    >>> 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 5 hosted by Hostway
    Stay green...Green IT