PHP
  Home arrow PHP arrow Page 6 - Generating View from MySQL to Simulate the Model-View-Controller Schema in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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!  



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT