PHP
  Home arrow PHP arrow Page 2 - 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 - The starting point of a brand new MVC schema: defining some MySQL processing classes


    (Page 2 of 6 )

    Since my intention here is to demonstrate how to generate several views from a single MySQL result set, logically the first step involved in developing this example consists of defining a couple of MySQL-related PHP classes. These classes can be used first for connecting to the database server, and then for obtaining the data sets that I mentioned before.

    That said, here are the corresponding signatures for these classes:

    // define 'MySQL' class
    class MySQL{
        private $conId;
        private $host;
        private $user;
        private $password;
        private $database;
        public function __construct($options=array()){
            if(count($options)<4){
                throw new Exception('Invalid number of connection
    parameters');
            }
            foreach($options as $parameter=>$value){
                if(!$value){
                    throw new Exception('Invalid parameter
    '.$parameter);
                }
                $this->{$parameter}=$value;
            }
            $this->connectDB();
        }
        private function connectDB(){
            if(!$this->conId=mysql_connect($this->host,$this-
    >user,$this->password)){
                throw new Exception('Error connecting to the
    server');
            }
            if(!mysql_select_db($this->database,$this->conId)){
                throw new Exception('Error selecting database');
            }
        }
        public function query($query){
            if(!$this->result=mysql_query($query,$this->conId)){
                throw new Exception('Error performing query
    '.$query);
            }
            return new Result($this,$this->result);
        }
    }
    // define 'Result' class
    class Result {
        private $mysql;
        private $result;
        public function __construct(&$mysql,$result){
            $this->mysql=&$mysql;
            $this->result=$result;
        }
        public function fetchRow(){
            return mysql_fetch_assoc($this->result);
        }
        public function countRows(){
            if(!$rows=mysql_num_rows($this->result)){
                throw new Exception('Error counting rows');
            }
            return $rows;
        }
        public function countAffectedRows(){
            if(!$rows=mysql_affected_rows($this->mysql->conId)){
                throw new Exception('Error counting affected rows');
            }
            return $rows;
        }
        public function getInsertID(){
            if(!$id=mysql_insert_id($this->mysql->conId)){
                throw new Exception('Error getting ID');
            }
            return $id;
        }
        public function seekRow($row=0){
            if(!int($row)||$row<0){
                throw new Exception('Invalid result set offset');
            }
            if(!mysql_data_seek($this->result,$row)){
                throw new Exception('Error seeking data');
            }
        }
        public function getResultSet(){
            return $this->result;
        }
    }

    Certainly the above classes should be quite familiar to you, since I've been using them in several cases over the course of different PHP tutorials that were published previously on the prestigious Developer Shed network. However, as I mentioned before, I need to have at my disposal a pair of simple MySQL processing classes to work with, in order to start defining a consistent MVC schema.

    Now, let's get rid of the boring stuff, and concentrate on something much more interesting. Once the previous classes have been defined, it's time to learn how to construct a brand new controller class. This class should be able to instruct the model (in this case represented by a MySQL data set) to generate different outputs or views from it. That sounds attractive indeed! Thus, jump straight into the upcoming section and learn how it will be done.

    More PHP Articles
    More By Alejandro Gervasio


       · 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 5 hosted by Hostway
    Stay green...Green IT