PHP
  Home arrow PHP arrow Page 5 - 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? 
Google.com  
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 - Source code ahead: completing the "displayRecords()" method
    ( Page 5 of 6 )

    Let's jump directly into the code to complete the method definition. Once the page pointer has been properly validated, it's time to fetch the exact number of records specified for being displayed on each page. Since we've already stored the total number of records as a class property $this->totalRecs, the process of extracting the corresponding records is a breeze. We just use the array_slice() PHP function to position the array internal pointer at the right place, and retrieve the correct number of records. This simple line extracts the records from the $this-> totalRecs array, as listed below:

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

    Now, if the page pointer $page is equal to 1, and assuming that the default number of records is 5, the $records variable will store the first five elements from the $this->totalRecs array. Similarly, if $page is equal to 2, the second set of five records will be stored. As you can see, the expression is simple but extremely powerful for extracting chunks of records, closely resembling the LIMIT SQL statement extensively used with MySQL.

    At this point, the method only needs to loop over the extracted records and progressively generate the output to be displayed in the browser. Using a couple of "foreach" loops, the task is nicely performed:

    foreach($records as $row){

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

    foreach($columns as $column){

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

    }

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

    }

    By now, we've applied very basic formatting to the records. However, this might be changed with minor modifications. Before I hear someone complaining about this, in Part III of this article, we'll cover in-depth record formatting to suit real-world needs. So, you don't have any excuses to skip the next part of the series!

    The rest of the code is nearly identical to the example shown in the first part. The methods take care of creating the paging links, first checking whether it's possible to insert a <previous> link. If it's possible, the corresponding link is generated. Then, the numbered links are created using a loop, and appending the $page variable to the querystring, providing the necessary navigational links.

    Finally, the methods verifies whether it's feasible to generate a <next> link, to completely display the paging links.

    Here are the lines used to create the paging links:

    // create previous link

    if($page>1){

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

    }

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

    }

    We've retrieved the records to be displayed and built the paging links with a single method. Definitely, the method shows its power and simplicity with some lines of code. Maybe we're not ready to paginate thousands of records, but this attempt is fairly valid!

    Well, our job is almost done by now. But, am I forgetting anything? Sure, we need to put the class into action and see its actual functionality. Therefore, let's write a bit more of code to implement our "Pager" class.



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

       

    PHP ARTICLES

    - 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...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





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