PHP
  Home arrow PHP arrow Page 4 - 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 - Putting the classes to work: a practical example
    ( Page 4 of 4 )

    To begin with, let’s set up a simple database to store the name of a few articles, and include one simple table. This table will contain three basic fields, called “articleid”, “name” and “site’, which reference respectively, the ID for each article, the story name, and finally the name of the site where the article was published. Sounds simple, doesn’t it?

    In first place, let’s create the table “articles”:

    CREATE TABLE articles

    (

    articleidINTUNSIGNED NOT NULLAUTO_INCREMENT PRIMARYKEY,

    name VARCHAR(60) NOT NULL,

    site VARCHAR(60) NOT NULL

    );

    As you can see, the table definition shows a simple structure where the first field is the table’s primary key, specified as being of the AUTO_INCREMENT type. The subsequent fields are defined to hold string values, as mentioned before: the article’s name and the site where it was originally published.

    What’s our next step? We need to populate the table with some data related to the articles, so let’s perform a multi-row INSERT operation, in order to fill the database table with records:

    INSERT INTO articles VALUES

    (NULL,"Regular Expressions in JavaScript","Devarticles.com"),

    (NULL,"Preloading HTML content withCSS","Devarticles.com"),

    (NULL,"Handling Events with theDOM- Part 1","Devarticles.com"),

    (NULL,"Handling Events with theDOM- Part 2","Devarticles.com"),

    (NULL,"Handling Events with theDOM- Part 3","Devarticles.com"),

    (NULL,"Output Caching with PHP","Devshed.com"),

    (NULL,"Introduction toCSSPositioning Properties Part 1","Devarticles.com"),

    (NULL,"Introduction toCSSPositioning Properties Part 2","Devarticles.com"),

    (NULL,"Introduction toCSSPositioning Properties Part 3","Devarticles.com"),

    (NULL,"Matching Div heights withCSSand JavaScript","Devarticles.com"),

    (NULL,"Customizing Styles: User-controlled Style Sheets Part 1","Devarticles.com"),

    (NULL,"Customizing Styles: User-controlled Style Sheets Part 2","Devarticles.com"),

    (NULL,"Customizing Styles: User-controlled Style Sheets Part 3","Devarticles.com"),

    (NULL,"Div-based layout withCSS","Devarticles.com"),

    (NULL,"Building friendly pop-up windows","Devarticles.com"),

    (NULL,"Building accessible web forms","Devarticles.com"),

    (NULL,"Building a Template Parser class with PHP - Part 1","Devshed.com"),

    (NULL,"Building a Template Parser class with PHP - Part 2","Devshed.com"),

    (NULL,"A quick look at Cross-Site Scripting","Devshed.com"),

    (NULL,"Email Address verification with PHP","Devshed.com"),

    (NULL,"CSSshorthand at a glance","Devarticles.com"),

    (NULL,"Creating pop-up notes withCSSand JavaScript","Devarticles.com"),

    (NULL,"Controllable Navigation bars with JavaSCript - Part 1","Devarticles.com"),

    (NULL,"Controllable Navigation bars with JavaSCript - Part 2","Devarticles.com");

    Okay, we’ve populated some records specifying NULL values for the “articleid” field, adding the corresponding story name, as well as the name of the site where the article was posted. Now, we have some data to play with. Let’s put the classes together to display some paged results. It’s as simple as this:

    // include the classes

    require_once 'mysqlclass.php';

    require_once 'pagerclass.php';

    // instantiate a MySQLConnector object

    $db=&new MySQLConnector('host','user','password','articles');

    // build query

    $sql="SELECT articleid,name FROM articles WHERE site='Devarticles.com'";

    // instantiate a Pager object that aggregates the “MySQLConnector object

    $pg=&new Pager($db,$sql);

    // display paged result set

    echo $pg->displayRecords($_GET['page']);

    With just a few lines of code we’re in business, displaying nicely paged records. If you look at the code above, you can see that we first included the class files, and then instantiated a “MySQLConnector” object, which handles all of the operations related to MySQL.

    After that, we build a regular SELECT statement to retrieve all of the articles that were published at “Devarticles.com”, and instantiate a “Pager” object, passing to it the query itself and the “MySQLConnector” object. Doing so, the second object aggregates the first one, for performing internally the given query and returning a paged result.

    By spicing up the output with some CSS declarations, this is what I get on my browser, after executing the above script:

    Probably this is not the “coolest” visual presentation, but the classes are doing their jobs quite well. We’ve obtained a decent paged result set in conjunction with all of the paging links. Isn’t aggregation remarkably powerful? I’m sure you’ll agree.

    Conclusion

    That’s all for now. Hopefully, our round trip exploring the core concepts and practical application of aggregation in PHP has been highly rewarding, and given you a more intimate grounding in the subject. However, this is only the start. There is plenty of room to experiment and find the right way to implement a powerful, well-defined object interaction in the real world. The more you learn about Object Oriented Programming, the faster your classes will get connected properly, saving you from the hard work of rewriting code. Once you have a few classes worked out, it’s as easy as building a wall with bricks. See you soon!



     
     
    >>> 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