PHP
  Home arrow PHP arrow Page 2 - Completing an Extensible Website Engine with PHP 5
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? 
Google.com  
PHP

Completing an Extensible Website Engine with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2006-10-31


    Table of Contents:
  • Completing an Extensible Website Engine with PHP 5
  • Pulling web page contents from a database table
  • Injecting web page contents into the template file
  • The website engine 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


    Completing an Extensible Website Engine with PHP 5 - Pulling web page contents from a database table
    ( Page 2 of 4 )

    Due to the fact that the whole website engine will be fetching web page contents straight from the “pages” database table that was originally defined in the first article, the initial step involved in this project consists of creating a simple mechanism that allows for interaction with MySQL.

    In response to the requirements mentioned above, I’ll define a couple of PHP classes tasked with handling all the processes related to connecting to MySQL and selecting databases, as well as executing SQL queries.

    The respective signatures of these two MySQL processing classes are listed below:

    // define ‘MySQL’ class
    class MySQL{
        private $conId;
        private $host;
        private $user;
        private $password;
        private $database;
        private $result;
        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 fetchFormattedResult($query,$closeTag='</p>'){
            if(preg_match("/^SELECT/",$query)){
                throw new Exception('Query must begin with SELECT');
            }
            $output='';
            $opentag=str_replace('/','',$endTag);
            while($row=$this->fetchRow()){
                $output.=$openTag.$row.$closeTag;
            }
            unset($openTag,$closeTag);
            return $output;
        }
    }

    If you take some time and examine the source code of the above two classes, then you’ll possibly find them familiar, since I’ve been using them in some of my previous PHP articles.

    On the other hand, if this doesn’t ring any bells to you, let me tell you that the first “MySQL” class is a simple wrapper. It's handy for connecting to the server, selecting a particular database, and running queries. The second one is responsible for performing a few useful tasks regarding the manipulation of result sets, like fetching and counting returned rows, and others.

    At this point, I can say that the pair of MySQL processing classes that you saw previously are the first building blocks of the website engine that I’m currently building. They will handle all the processes required for fetching web page contents from the “pages” database table. Sounds logical, right?

    Now, as you’ll probably recall, the entire presentation layer that corresponds to this website engine was completely handled by a single template file called “default_template.htm,” which also was created over the course of the first tutorial of the series. Keeping in mind this condition, it’s obvious that some kind of mechanism for processing that template is required here.

    In order to tackle all the tasks for parsing the mentioned template file, in the following section I’ll be defining a useful template processing class with PHP 5. It will be charged first with taking up all the web page data pulled from the corresponding database table, and second with injecting this data straight into the respective template file.

    After defining this template processing class, the PHP 5-based website engine that I originally planned to create will be near to completion, so hurry up and read the following section!



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek