Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 55,113 times

Contents

Downloads

Related Categories

Implementing a template based website - The PHP Code

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:

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.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • using include with a php marked-up htm template

    Posted by aaron123nz on 02 Jun 2005

    I have been trying to use php's inlude to include a template file which has been marked up with some php variables. However when using include() the php parsing drops out. This code keeps the parsin...

  • Code Errors

    Posted by jaam on 25 Feb 2004

    This sample is great BUT have a cuople of errors.
    The file test.php is missing "#include template.php";.
    The file template.php presents a variable $name that must be repalced with $templatename.
    If...

  • Posted by James Crowley on 22 Feb 2004

    The article was written before PHP had made this a requirement - I'll try and get the article updated to reflect these changes. :)

  • Posted by James Crowley on 22 Feb 2004

    The article demonstrates two methods - one that uses a MySQL database to store the templates, and one that just uses a folder on the website.

  • Don't understand

    Posted by johnlcox on 26 Nov 2003

    I'm having a really hard time understanding how this works and I am not able to test it since I don't have a mysql server setup on my computer. Does anyone have an example they could show me that doe...