PHP
  Home arrow PHP arrow Page 4 - 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 - The website engine in action
    ( Page 4 of 4 )

    Although I showed you in separate pieces how the contents of the respective “page” database table can be used for creating dynamic web pages, the real power of the website engine developed here is revealed when all the previous classes are put to work on the same PHP file.

    To illustrate more clearly the entire process for generating the different web pages that I showed you in the first article, say you want to implement the website engine on just one file, called “index.php.” Based on this premise, this file would look as follows:

    try{
        // include class files
        require_once 'classes/template_processor_class.php';
        require_once 'classes/mysql_class.php';
        $pageid=!$_GET['pageid']?1:$_GET['pageid'];
        // connect to MySQL
        $db=new MySQL(array('host'=>'host','user'=>'user','password'=>'password',
    'database'=>'mysite'));
        // fetch page contents from database
        $result=$db->query("SELECT * FROM pages WHERE id=$pageid");
        if(!$result->countRows()){
            throw new Exception('Error fetching page contents');
        }
        $row=$result->fetchRow();
        // build array of tags
        $tags=array('title'=>$row['title'],'header'=>$row
    ['header'],'leftcolumn'=>$row['leftcol'],'centercolumn'=>$row
    ['centercol'],'rightcolumn'=>$row['rightcol'],'footer'=>$row
    ['footer']);
        //instantiate template processor object
        $tpl=new TemplateProcessor($tags);
        // display compressed page
        echo $tpl->getHTML();
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    Believe or not, this is all the PHP code that you need to create the different web pages that compose the whole website. If you dissect the above script into pieces, you’ll see it first includes all the classes that were created previously, then connects to MySQL and finally fetches from the “pages” database table all the web document contents that correspond to a specific value of the “$pageid” GET variable. Wasn’t that easy?

    Of course, once page data has been properly returned to the script, each section of the web document (that is the header, the left, right and center columns, and the footer respectively) is dynamically generated by using the template processor class. With reference to this process in particular, the following line:

    $tags=array('title'=>$row['title'],'header'=>$row
    ['header'],'leftcolumn'=>$row['leftcol'],'centercolumn'=>$row
    ['centercol'],'rightcolumn'=>$row['rightcol'],'footer'=>$row
    ['footer']);

    demonstrates in a clear fashion how each web page section is created on the fly with contents pulled from the “pages” database table. Now, do you see how the website engine works with only one “$pageid” parameter?

    Just think about working with more tables, which can be filled via your favorite CMS. Certainly, possibilities are numerous!

    Final thoughts

    In this two-part series, I gave you some useful pointers on how to build an expandable website engine that uses PHP 5 as the primary scripting language for generating web pages on the fly. Even when the system may not fit all your needs, I think it can be considered a good option, particularly when you want to create a database-driven website without having to work with multiple databases.

    As usual, see you in the next PHP tutorial!



     
     
    >>> 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek