PHP
  Home arrow PHP arrow Page 2 - Previous or Next? Paginating Records with PHP, part 1
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? 
Google.com  
PHP

Previous or Next? Paginating Records with PHP, part 1
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 22
    2005-04-26


    Table of Contents:
  • Previous or Next? Paginating Records with PHP, part 1
  • The first step: paginating records in a procedural way
  • A deeper look at the "paginatetRecords()" function
  • Putting the "paginateRecords()" function into action: procedural record paging

  • 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 1 - The first step: paginating records in a procedural way
    ( Page 2 of 4 )

    Our first method for paginating records might be easily developed by taking a procedural approach: using a flat text file as the data source for displaying information, and defining a PHP function that takes care of paginating the records. Sounds like something good to deploy, at least for the initial step, right?

    Let's begin defining a simple text file, which contains several lines (our records), including information about a song's author and the song's title. The example file, which is called "data.dat," could be structured as listed below:

    Chariots of fire|Vangelis

    Friends of Mr. Cairo|Vangelis

    Lucifer|The Alan Parson Project

    The eye in the sky|Alan Parson Project

    Caribean Blue|Enya

    Only Time|Enya

    Music|John Miles

    The turn of a friendly card|The Alan Parson Project

    Children|Robert Miles

    One and One|Robert Miles

    Sadness|Enigma

    Mea Culpa|Enigma

    Cherry Blossom Girl|Air

    Hot stuff|Donna Summer

    Bad Girls|Donna Summer

    I will survive|Gloria Gaynor

    Rimes and reasons|John Denver

    Touch and go|Emerson,Lake and Powell

    In Jeopardy|Roger Hogdson

    Ameno|Era

    As you can see, our text file containing information about several songs has been created, defining two fields for each line, author and title respectively, separated by a pipe ("|") character. This way we've established the overall structure for this simple flat database. Please don't start complaining about my musical preferences, because they're beyond the scope of this article. Let's focus our attention on the code listed above.

    Once we have defined the data source file, we need to create a PHP function for retrieving the records from the file, and generating the corresponding paginating links. In fact, the process is pretty straightforward, as we'll see in a moment. Now, let's start writing the function to paginate records. I've named it "paginateRecords()", and its definition is the following:

    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;

    }

    Don't feel intimidated by the function's source code, since it's really easy to understand. In order to grasp the underlying logic, let's break down the code to have a detailed look at each section. Are you ready? Let's go for it.



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

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek