Home arrow PHP arrow Page 6 - Generating View from MySQL to Simulate the Model-View-Controller Schema in PHP

Putting the classes to work together: seeing the MVC schema in action - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I expressed in the previous section, here is an example that shows how to use all the classes that I coded before, assuming that there's a database table called "users" populated with the following data:

ID: 1 Name: user1 Email: user1@domain.com
ID: 2 Name: user2 Email: user2@domain.com
ID: 3 Name: user3 Email: user3@domain.com
ID: 4 Name: user4 Email: user4@domain.com
ID: 5 Name: user5 Email: user5@domain.com

Now, pay attention to the code snippet below, which returns the previous data set as XHTML:

// return result set as XHTML
// connect to MySQL
$db=new MySQL(array
('host'=>'host','user'=>'user','password'=>'password',
'database'=>'database'));
// get a result set
$result=$db->query('SELECT * FROM users');
$resultController=new ResultController('xhtml');
$mysqlResult=new MySQLResult($resultController,$result);
$outputGenerator=new outputGenerator($mysqlResult);
echo $outputGenerator->generateOutput();
/*

displays the following:

ID: 1 Name: user1 Email: user1@domain.com
ID: 2 Name: user2 Email: user2@domain.com
ID: 3 Name: user3 Email: user3@domain.com
ID: 4 Name: user4 Email: user4@domain.com
ID: 5 Name: user5 Email: user5@domain.com
*/

Finally, here are the two scripts that return the previous MySQL result set as XML and plain text respectively:

// return result set as XML
// connect to MySQL
$db=new MySQL(array
('host'=>'host','user'=>'user','password'=>'password',
'database'=>'database'));
// get a result set
$result=$db->query('SELECT * FROM users');
$resultController=new ResultController('xml');
$mysqlResult=new MySQLResult($resultController,$result);
$outputGenerator=new outputGenerator($mysqlResult);
echo $outputGenerator->generateOutput();

/* displays the following:

<users>
   
<user>
        <id>1</id>
        <name>user1</name>
        <email>user1@domain.com</email>
    </user>
    <user>
        <id>2</id>
        <name>user2</name>
        <email>user2@domain.com</email>
    </user>
    <user>
        <id>3</id>
        <name>user3</name>
        <email>usuario1@domain.com</email>
    </user>
    <user>
        <id>4</id>
        <name>user4</name>
        <email>usuario4@domain.com</email>
    </user>
    <user>
        <id>5</id>
        <name>user5</name>
        <email>usuario5@domain.com</email>
    </user>
</users>
*/

 // return result set as plain text
// connect to MySQL
$db=new MySQL(array
('host'=>'host','user'=>'user','password'=>'password',
'database'=>'database'));
// get a result set
$result=$db->query('SELECT * FROM users');
$resultController=new ResultController('plain');
$mysqlResult=new MySQLResult($resultController,$result);
$outputGenerator=new outputGenerator($mysqlResult);
echo $outputGenerator->generateOutput();

/*
displays the following:

ID: 3 Name: user1 Email: user1@domain.com
ID: 4 Name: user2 Email: user2@domain.com
ID: 5 Name: user3 Email: user3@domain.com
ID: 6 Name: user4 Email: user4@domain.com
ID: 7 Name: user5 Email: user5@domain.com
ID: 8 Name: user6 Email: user6@domain.com
*/

Final thoughts

In this last part of this series, you've hopefully learned how to apply the MVC schema with PHP to handle MySQL data sets and generate different views or outputs from it. As I said before, this is only an introduction to this huge subject, therefore if you want to go deeper into it, there's plenty of information on the web. See you in the next PHP tutorial!  



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap

Dev Shed Tutorial Topics: