PHP
  Home arrow PHP arrow Page 4 - 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? 
Google.com  
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 - Going deeper: a detailed coverage at the "displayRecords()" method
    ( Page 4 of 5 )

    Undoubtedly, the "displayRecords()" method is the real workhorse of the class, since it performs the hard work of building the SQL query, obtaining a formatted result set, and creating the paging links. While this might sound complex, the truth is that the process is quite simple.

    Based on the previous "Pager" class, defined in the second part of this series, the method uses the same logic, accepting only one parameter: $page, which simply behaves as a page pointer for displaying records in a paginated manner.

    Taking into account that the constructor has already set up the $query property, it's relatively easy to build dynamically a new query, adding a regular LIMIT clause to specify the offset and the number of records extracted from the database. So, let's put theory to the side for a moment and show the initial definition for our "displayRecords()" method:

    function displayRecords($page){

    // calculate total of records

    if(!$totalRecs=mysql_num_rows(mysql_query($this->query))){

    die('Cannot retrieve records from database');

    }

    // calculate number of pages

    $numPages=ceil($totalRecs/$this->numRecs);

    // validate page pointer $page

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

    $page=1;

    }

    // get result set

    $result=mysql_query($this->query.' LIMIT '.($page-1)*$this->numRecs.','.$this->numRecs);

    }

    As the above code clearly shows, the method is fed with the "$page" page pointer. Then, it calculates the total number of records extracted, performing the query without any LIMIT clause, and later using the "mysql_num_rows()" PHP function. The reason for doing this should become clear, because we need to calculate the number of pages needed to span all of the records. If the method cannot obtain this value, the class is simply stopped.

    The next step logically involves calculating the number of pages to span the whole result set. It means simply dividing the total number of records by the number or records per page. Once this is done, the result is rounded up to work with an integer value.

    Finally, having obtained these values, the method is capable of performing a strong validation process on the $page pointer, allowing only an integer positive value, which is always less than the number of pages previously calculated. If the page pointer is not within the valid range, for any given reason, then we place it at the first page ($page=1). Here's the code to validate the page pointer:

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

    $page=1;

    }

    I briefly explained the reason for this in the second part of the series. Why should we bother validating this parameter? The answer is straightforward: we don't want any malicious code injected into the querystring, troubling our result sets or even the paging links. As with any parameter coming from GET, POST methods, it must be properly checked and validated. While this sounds like common sense, there are lots of sites that are extremely vulnerable to querysting manipulation, to name just one security failure. So, always keep an eye on that, and remember that the Internet is largely out of your control!

    Back to our method explanation, the only remaining step is to perform a SELECT query appending a usual LIMIT clause, to obtain the specified number of records to be displayed at a time. The line listed below does that:

    $result=mysql_query($this->query.' LIMIT '.($page-1)*$this->numRecs.','.$this->numRecs);

    Now we've obtained the proper result set, ready to be displayed before the visitor's eyes. However, the method is still immature in its current version, since we have not used the template file to replace the placeholders and give our records the desired look. What's more, we need to include the paging links in the final output. The method must be completed. Let's move on and show how to work with the template file and our paging links.



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

       

    PHP ARTICLES

    - 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...
    - Mastering WHILE Loops for PHP and MySQL





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