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

       · 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