PHP
  Home arrow PHP arrow Page 4 - 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 - 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


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