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


       · In the last installment of this series, you'll learn how to implement a basic MVC...
     

       

    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