PHP
  Home arrow PHP arrow Page 3 - Generating Outputs from MySQL with Static Members and Methods in PHP 5
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

Generating Outputs from MySQL with Static Members and Methods in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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
     

       

    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