PHP
  Home arrow PHP arrow Page 3 - Generating Outputs from MySQL with Sta...
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

Generating Outputs from MySQL with Static Members and Methods in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2006-10-09

    Table of Contents:
  • Generating Outputs from MySQL with Static Members and Methods in PHP 5
  • Working with MySQL
  • Creating the ResultToString, ResultToXML and ResultToArray classes
  • Defining the ResultProcessorFactory class

  • 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


    Generating Outputs from MySQL with Static Members and Methods in PHP 5 - Creating the ResultToString, ResultToXML and ResultToArray classes


    (Page 3 of 4 )

    As the title of this section indicates, my intention consists basically of building up three different classes, where each of them will be tasked with taking up a given MySQL result set. They will then apply to it the proper conversion process; that is, they will turn the data set rows into plain strings, XML data and an associative array respectively. Finally, they will return the results to calling code.

    All right, after explaining the main purpose of each one of the classes that I plan to create, it’s time to see how they look, right? I have listed their source code below, so check them out:

    // define 'ResultToString' class

     

    class ResultToString{
    private $result;
    public function
    __construct(Result $result){
          
    $this->result=$result;
    }
    // fetch result set as plain text
    public function fetch(){
    $str='';
          
    while($row=$this->result->fetchRow()){
    foreach($row as $key=>$value){
            
    $str.='['.$key.']='.$value."n";
    }
             $str.='--------------------------'."n";
    }
    return $str;
    }
    }
    // define 'ResultToXML' class
    class ResultToXML{
    private $result;
    public function
    __construct(Result $result){
           $this->result=$result;
    }
    // fetch result set as XML
    public function fetch(){
    $xml='<?xml
    version="1.0"
    encoding="iso-8859-1"?>'."n".'<data>'."n";
    

     

            while($row=$this->result->fetchRow()){        $xml.='<row>'."n"; foreach($row as $key=>$value){               $xml.='<'.$key.'>'.$value.'</'.$key.'>'."n"; }            $xml.='</row>'."n"; }         $xml.='</data>'."n"; return $xml; } } // define 'ResultToArray' class class ResultToArray{ private $result; public function __construct(Result $result){        $this->result=$result; } // fetch result set as array public function fetch(){ $data=array();       while($row=$this->result->fetchRow()){ $data[]=$row; } return $data; } }

     

    As you’ll realize, the logic of the three classes shown above is fairly easy to understand. Essentially, each of them will accept an object of type “Result” inputted via the corresponding constructor. The object will be used from inside the class to convert a given MySQL result set to the corresponding format, that is plain strings, XML or an associative array.

    With reference to the conversion process in question, this will be performed in all the previously defined classes by using their “fetch()” method, which obviously implements a different logic in accordance with the type of data that must be returned to calling code. Simple and efficient, isn’t it?

    Okay, at this point I defined the pair of MySQL processing classes that you saw in a previous section, as well as the three that were created a few lines above and are handy for generating several outputs from a specific result set. However, until now I've not given you a single clue about how and why we will include static methods within the classes just defined. So, where will they be coded?

    Fortunately, I have a good answer for that question. Since my primary objective is to generate disparate data formats from a single MySQL result set, the next step that I’m going to take will consist of creating an additional class, which will be capable of returning distinct data set processor objects  to produce the range of outputs that I mentioned before.

    How will this be done? Yep, you guessed right. The entire process for returning result set processor objects to calling code will be handled by a static method! After all, we’re not far from reaching our goal, but this requires you to jump straight into the next section and continue reading. Thus, go ahead. I’ll be there, waiting for you.

    More PHP Articles
    More By Alejandro Gervasio


       · This final article of the series is pointed out to demonstrate how to use static...
     

       

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