HomePHP Page 4 - Completing an Extensible Website Engine with PHP 5
The website engine in action - PHP
Are you one of those web developers searching for a comprehensive approach to constructing an expandable website engine with PHP 5? If your answer is a resounding yes, then this series might appeal to you. Welcome to the final part of the series “Building an extensible website engine with PHP 5.” In two tutorials, this series teaches you how to build a versatile website system which allows you to generate dynamic web documents by using a few simple PHP classes.
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:
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.