HomePHP 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.
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.