PHP
  Home arrow PHP arrow Page 3 - Object Interaction in PHP: Introduction to Aggregation, part 4
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 4
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 23
    2005-06-15


    Table of Contents:
  • Object Interaction in PHP: Introduction to Aggregation, part 4
  • Assembling classes: a brief look at the “MySQLConnector” class
  • Assembling classes (continued): the “Pager” class at glance
  • Putting the classes to work: a practical example

  • 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 4 - Assembling classes (continued): the “Pager” class at glance
    ( Page 3 of 4 )

    I’m not going to explain once again how this class works, because it was thoroughly covered in the previous articles in this series. Just let me to say that it aggregates the “MySQLConnector” object, accepting it as a parameter. It later uses the methods offered by this first object to execute SQL queries, display paged result sets and finally, generate the proper paging links.

    As usual, here’s the source code:

    class Pager {

                var $db; // MySQLConnector object

                var $query; // SQL query

                var $numRecs; // number of records per page

                var $output=''; // dynamic output

                function Pager(&$db,$query,$numRecs=5){

                            $this->db=&$db;

                            (preg_match("/^SELECT/",$query))?$this->query=$query:die('Invalid query '.$query);

                            (is_int($numRecs)&&$numRecs>0)?$this->numRecs=$numRecs:die('Invalid number of 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 />';

                            }

                            $this->output.='<div>';

                            // create previous link

                            if($page>1){

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

                            }

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

                            }

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

                            // return generated output

                            return $this->output;

                }

    }

    The only thing to be pointed out here is how we use the methods of the “MySQLConnector” object to perform the tasks inherent to the “Pager” class. Generally speaking, as a rule of thumb, when one object aggregates another one, the first object is passed as a reference to the second, in order to address some performance issues.

    Of course, the possible overhead caused to the server will depend strongly on how applications use the objects involved in the interaction process. For instance, if multiple objects are sharing the same object for connecting to a database, probably the server will suffer a heavy load when the application is used by numerous users. While this may sound like a common sense thing, it’s important to know where to implement aggregation in projects.

    Having listed the source code for each developed class, it’s time to build an illustrative example for proper implementation. Just turn the page and keep reading.



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