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
(Page 6 of 6 )
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!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |