PHP
  Home arrow PHP arrow Page 5 - Generating View from MySQL to Simulate the Model-View-Controller Schema in PHP
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 View from MySQL to Simulate the Model-View-Controller Schema in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 14
    2006-08-21


    Table of Contents:
  • Generating View from MySQL to Simulate the Model-View-Controller Schema in PHP
  • The starting point of a brand new MVC schema: defining some MySQL processing classes
  • Setting up the basics for generating disparate views: defining a controller based on MySQL datasets
  • Defining the next link of the chain: creating a model founded on native MySQL data sets
  • Generating distinct views from a single MySQL result set: creating an output generator class
  • Putting the classes to work together: seeing the MVC schema in action

  • 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 View from MySQL to Simulate the Model-View-Controller Schema in PHP - Generating distinct views from a single MySQL result set: creating an output generator class
    ( Page 5 of 6 )

    As you might have guessed, the last PHP class responsible for representing a View element is the one tasked with generating the different outputs, that is plain, XHTML or XML, based on a single MySQL result set.

    As you'll see, the class in question will take up an object of type "MySQLResult" (in this case it's called "a model"), and implement the required programming logic to produce the range of outputs mentioned before.

    Regarding the definition of this class, its source code is listed below. Take a look:

    // define 'OutputGenerator' class (View)
    class OutputGenerator{
        private $mysqlResult;
        public function __construct(MySQLResult $mysqlResult){
            $this->mysqlResult=$mysqlResult;
        }
        // generate outputs
        public function generateOutput(){
            // get result set
            $result=$this->mysqlResult->getResultSet();
            switch ($this->mysqlResult->getControllerOutput()){
                case 'xhtml':
                    // return result set as XHTML output
                    $xhtml='<!doctype html public "-//W3C//DTD XHTML
    1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
    transitional.dtd"><html><head><title>XHTML-based
    output</title><head><body>';
                    while($row=mysql_fetch_assoc($result)){
                        $xhtml.='<p>ID: '.$row['id'].' Name: '.$row
    ['name'].' Email: '.$row['email'].'</p>';
                    }
                    $xhtml.='</body></html>';
                    return $xhtml;
                case 'xml':
                    // return result set as XML output
                    header('Content-Type: text/xml; charset=iso-8859-
    1');
                    $xml='<?xml version="1.0" encoding="iso-8859-1"?
    >';
                    $xml.='<users>'."n";
                    while($row=mysql_fetch_assoc($result)){
                        $xml.='<user><id>'.$row
    ['id'].'</id><name>'.$row['name'].'</name><email>'.$row
    ['email'].'</email></user>'."n";
                    }
                    $xml.='</users>';
                    return $xml;               

                case 'plain':
                    // return result set as plain text
                    $textout='';
                    while($row=mysql_fetch_assoc($result)){
                        $textout.='<p>ID: '.$row['id'].'Name: '.$row
    ['name'].'Email: '.$row['email']."n";
                    }
                    return $textout;
            }
        }
    }

    As you can see, the class shown above accepts an object of type "MySQLResult" as the unique parameter and uses it to generate a specific view, which was selected initially by the respective controller object. Here, undoubtedly the most significant method that the class exposes is "generateOutput()", which produces the different views, based on a single MySQL result set, obtained by the "getResultSet()" method.

    Indeed, the logic followed by this new class is quite straightforward, and it shouldn't be difficult to understand. All of its functionality is focused on generating diverse outputs, in accordance with the instructions given by the controller object.

    Okay, at this point, after showing the signature of the "OutputGenerator" class, it's clear to see how easy it is to implement a basic MVC schema with PHP, which uses three independent classes to obtain several views from a given MySQL data set. However, I'm sure that you want to see how these classes interoperate with each other to perform the tasks that I mentioned before.

    Based on this premise, in the last section of this article, you'll see them working together. Go ahead and read the next few lines. I'll be there, waiting for you.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek