Implementing a template based website

The PHP Code

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:

    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.

You might also like...


About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - Rick Osborne