PHP
  Home arrow PHP arrow Page 3 - Object Interaction in PHP: Introduction to Aggregation, 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

Object Interaction in PHP: Introduction to Aggregation, part 3
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 18
    2005-06-08


    Table of Contents:
  • Object Interaction in PHP: Introduction to Aggregation, part 3
  • Moving back and forth: building a paging class
  • Refactoring the “displayRecords()” method
  • Completing the refactoring process: building the paging links

  • 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


    Object Interaction in PHP: Introduction to Aggregation, part 3 - Refactoring the “displayRecords()” method
    ( Page 3 of 4 )

    Width no doubt, this method stand on its shoulders the hard work of the class, since it must perform the proper SQL query against the database, retrieve the records for being displayed, and finally create the paging links. What a though work! However, due to the great capabilities that aggregation fully brings to this method, all of those tasks are performed with minor complications. This significantly reduced overload is a straight consequence of having at our disposal the methods offered by the “MySQLConnector” object.

    Thus, let’s take a look at the original definition for this method, so we’re able to understand how it does its thing. Here’s our first glance at its structure, to perform the SQL query and generate the dynamic output from the database records:

    function displayRecords($page){
      // calculate total number of records using MySQLConnector object
      if(!$totalRecs=$this->db->getNumRows($this->db->performQuery($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;
        }
      // retrieve result set using MySQLConnector object
      $this->db->performQuery($this->query.' LIMIT '.($page-1)*$this->numRecs.','.$this->numRecs);
      while($rows=&$this->db->fetchRow()){
        foreach($rows as $row){
        $this->output.=$row.'&nbsp;';
        }
      $this->output.='<br />';
      }
    }

    The method accepts a single parameter $page, which simply behaves as a page pointer to determine how to display the paged records and build up the corresponding paging links. Certainly, this logic has already been implemented in previous articles that cover completely paging mechanisms. In order to get a better understanding, you may want to read the previous articles for a full review of this technique.

    However, let’s focus our attention now on the rest of the method’s code. The methods really need to calculate the total number of records returned by the query, to create the paging links. Remember that our “MySQLConnector” object is available inside the class? Good, because we use its own methods, that is, “getNumRows()” and “performQuery()”, to calculate the number of records and execute the SQL query. Isn’t it great? Please notice the below lines:

    // calculate total number of records using MySQLConnector object
    if(!$totalRecs=$this->db->getNumRows($this->db->performQuery($this->query))){
        die('Cannot retrieve records from database');
    }

    The above code obtains the total number of records, using the methods generously offered by the MySQLConnector object. In first instance, the method perform the SQL query invoking directly to “performQuery()”, as listed below:

    $this->db->performQuery($this->query)

    And next, the expression is completed by calling the method “getNumRows()”, like this:

    $totalRecs=$this->db->getNumRows($this->db->performQuery($this->query))

    At this point, I guess you’ve grasped the underlying idea behind Aggregation. In both expressions we’re using the methods of the “MySQLConnector object (referenced as $this->db) to fit the purposes of the a “Pager” object. That’s what I’d call it a good friend!

    The next section of the code validates the page pointer to make sure is an appropriate value, avoiding some possible tampering attempts directly manipulating URL’s, in the following manner:

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

    The expression checks to see the value of the $page pointer. If its value is lesser than 1 or greater than the number of pages needed to show the whole result set, then it’s simply set to 1.

    The rest of the code uses again the methods of the “MySQLConnector” object, “performQuery()” and “fetchRow()” respectively, in order to get a dynamic paged result set adding a LIMIT clause to the original query, lastly looping over the records to generate the final output:

    $this->db->performQuery($this->query.' LIMIT '.($page-1)*$this->numRecs.','.$this->numRecs);
    while($rows=&$this->db->fetchRow()){
        foreach($rows as $row){
            $this->output.=$row.'&nbsp;';
       
        }
        $this->output.='<br />';
    }

    Notice that once the records are stored in the class property $this->output, we apply to them some kind of basic visual formatting. However, this might be quickly modified to accept a rather more polished visual presentation.

    Okay, let’s hold our breath and recapitulate about what we’ve done until now. The method has already partially generated the dynamic output, by retrieving database records, using only the methods provided by the “MySQLConnector” object. Is there something pendent in our task list? Sure! We need to complete the source code, adding the section that builds the paging links. Let’s not waste more time and face properly that part.



     
     
    >>> 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek