MySQL
  Home arrow MySQL arrow Page 4 - Implementing a Template Based Web Site Wit...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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? 
MYSQL

Implementing a Template Based Web Site With PHP
By: James Crowley
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2002-04-09

    Table of Contents:
  • Implementing a Template Based Web Site With PHP
  • Templates in a database
  • Templates in a folder
  • The PHP Code
  • Conclusion

  • 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


    Implementing a Template Based Web Site With PHP - The PHP Code


    (Page 4 of 5 )

    Our demonstration PHP page is going to return the HTML for ResultsPage, along with a table full of results (with title and hits columns) using the information from the database. If you want to create the table and dummy data for the database, use the following MySQL statement:

    CREATE TABLE items (

    id int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,

    title varchar(30) NOT NULL,

    hits int NOT NULL DEFAULT '',

    );

    INSERT INTO items (title,hits) VALUES ('Test Item 1',29),('Test Item 2',15),('Test Item 3',0);


    Lets now take a look at the PHP code required to do this:

    <?

    #set the page title and query

    $pagetitle = "Search Results";

    $searchquery = "Dummy search";

    #get the database information

    $query = mysql_query("SELECT title,hits FROM items ORDER BY hits DESC LIMIT 10")

    #loop through the results

    while ($data=mysql_fetch_array($query)) {

    #load the template for this instance, appending the data onto $resultbits

    eval("\$resultbits .= \"".gettemplate("ResultsBit")."\";");

    }

    #now return the whole template

    eval("echo \"".gettemplate("ResultsPage")."\";");

    ?>


    The code is actually very simple: it sets a few variables, loads the data from a template, and then outputs the data. However, it's those eval statements that do all the hard work and need looking at more closely.

    Lets take a look at

    eval("\$resultbits .= \"".gettemplate("ResultsBit")."\";");

    first. What the eval statement does is evaluate PHP code as if you have written it yourself. For example

    eval("echo \"thetext\";");

    does the same as hard-coding

    echo "thetext";

    Our code takes advantage of this functionality, by writing it's own php code for this statement. What's the point, you may ask? Well, just as

    echo "hello $name";

    would output hello and the contents of the variable, $name, the same applies when used within an eval statement. Lets imagine that the gettemplate function returns simply

    <td>$data[title]</td>

    What ends up being evaluated in the call to the eval statement is (remembering that we need to escape characters such as $ and " within the string):

    $resultbits .= "<td>$data[title]</td>";

    PHP then appends <td>$data[title]</td> to the $resultbits variable, and replaces $data[title] with its current value. In the same way, PHP replaces all the variables within our template with the current value, which, in this instance, has been loaded from $data=mysql_fetch_array($query).

    Our code simply loops through the information from the database, evaluating the template for each different value of $data[title] and appending that value to $resultbits. Finally, once the loop is complete, we call another eval statement, this time retrieving the value from the ResultsPage template and replacing $pagetitle, $searchquery and $resultbits with their values in the script. Simple.

    More MySQL Articles
    More By James Crowley


     

    MYSQL ARTICLES

    - MySQL and BLOBs
    - Two Lessons in ASP and MySQL
    - Lord Of The Strings Part 2
    - Lord Of The Strings Part 1
    - Importing Data into MySQL with Navicat
    - Building a Sustainable Web Site
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - PhpED 3.2 – More Features Than You Can Poke ...
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - Security and Sessions in PHP
    - Setup Your Personal Reminder System Using PHP
    - Create a IP-Country Database Using PERL and ...
    - Developing a Dynamic Document Search in PHP ...







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