Home arrow PHP arrow Page 5 - 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 - PHP

Are you a curious PHP developer, wanting to learn how to simulate a basic Model-View-Controller schema with PHP? Then, look no further, because you’re in the right place. Welcome to the final part of the series “Simulating the Model-View-Controller Schema in PHP.” In three tutorials, this series provides you with a comprehensive introduction, aimed at implementing a MVC-based relationship between PHP objects.

  1. Generating View from MySQL to Simulate the Model-View-Controller Schema in PHP
  2. The starting point of a brand new MVC schema: defining some MySQL processing classes
  3. Setting up the basics for generating disparate views: defining a controller based on MySQL datasets
  4. Defining the next link of the chain: creating a model founded on native MySQL data sets
  5. Generating distinct views from a single MySQL result set: creating an output generator class
  6. Putting the classes to work together: seeing the MVC schema in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 14
August 21, 2006

print this article



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){
    // generate outputs
    public function generateOutput(){
        // get result set
        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-
                    $xhtml.='<p>ID: '.$row['id'].' Name: '.$row
['name'].' Email: '.$row['email'].'</p>';
                return $xhtml;
            case 'xml':
                // return result set as XML output
                header('Content-Type: text/xml; charset=iso-8859-
                $xml='<?xml version="1.0" encoding="iso-8859-1"?
                return $xml;               

            case 'plain':
                // return result set as plain text
                    $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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: