PHP
  Home arrow PHP arrow Page 4 - Completing an Extensible Website Engin...
Dev Shed Forums 
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

Completing an Extensible Website Engine with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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


    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!


    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.

       · The final part of the series demonstrates how to create a fully-functional website...
       · I tried to create an account here but I never receive my password...so-- I'm a...
       · Thank you for posting your comments on my PHP article, and I'm glad to know you're...
       · I've been playing with this for a while and it's outputting a blank page, view...
       · Hello there,Thank you for posting your feedback here. Now, concerning your...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - 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





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT