New to PHP page Templates... Got a simple question (error)..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mshirley
Forum Newbie
Posts: 3
Joined: Thu May 20, 2004 6:18 pm

New to PHP page Templates... Got a simple question (error)..

Post by mshirley »

I set up a very simple database and two php files to test a template after reading an article online...http://www.devshed.com/c/a/PHP/Database ... ng-Engine/

So I did it exactly like this tutorial showed...

But I get an error:

Parse error: parse error, unexpected T_VARIABLE in ../Docs/test/php/template.php on line 7

Fatal error: Call to undefined function: pageheader() in ../Docs/test/php/index.php on line 22

WHAT DID I MISS? :?:

Here's the text of what I'm using...

TEMPLATE DB:

[php_man]# phpMyAdmin MySQL-Dump
# version 2.4.0
# http://www.phpmyadmin.net/ (download page)
#
# Host: localhost
# Generation Time: May 20, 2004 at 06:13 PM
# Server version: 4.0.16
# PHP Version: 4.2.4-dev
# Database : `template`
# --------------------------------------------------------

#
# Table structure for table `templates`
#

CREATE TABLE templates (
templateid int(10) NOT NULL auto_increment,
name varchar(30) NOT NULL default '',
content text NOT NULL,
PRIMARY KEY (templateid)
) TYPE=MyISAM;

#
# Dumping data for table `templates`
#

INSERT INTO templates VALUES (1, 'page_header', '<!DOCTYPE HTML PUBLIC "−//W3C//DTD HTML 4.01\nTransitional//EN">rn<html>rn<head>rn\n<title>$pageTitle</title>rn</head>rn<body>');
INSERT INTO templates VALUES (2, 'page_footer', '</body>rn</html>');

[/php_man]



TEMPLATE.PHP

Code: Select all

<?php
function fetchTemplate ($templateName) {
$Query = "select content from templates where
name='$templateName'";
$tRes = mysql_query($Query);
$Template = mysql_fetch_array($tRes);
$Content = "echo "$Template[content]";";
return $Content;
}
function pageHeader($Title) {
eval (fetchTemplate("page_header"));
}
function pageFooter() {
eval (fetchTemplate("page_footer"));
}
?>

INDEX.PHP

Code: Select all

<?php
// Simple Templating Engine
// using a database tutorial.
// Written by Matt Eunson
// Define database variables you have to change these
$Server = "localhost";
$Username = "***";
$Pass = "****";
$Database = "template";
// Now connect to the database
mysql_connect($Server, $Username, $Pass) or
die("Couldn't connect to database.");
mysql_selectdb($Database) or die("Couldn't select
database: $Database");
// Include the template functions
include_once "template.php";
// Start our page
$Title = "My Site";
pageHeader($Title);
// Some content here...
echo "Welcome to my site.";
// And close the page
pageFooter();
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$Content = "echo "$Template[content]";";
to

Code: Select all

$Content = $Template['content'];
mshirley
Forum Newbie
Posts: 3
Joined: Thu May 20, 2004 6:18 pm

Post by mshirley »

I tried that this AM, just out of curiousity did you test this?

Now I get this error...

Parse error: parse error, unexpected '<' in ../Docs/test/php/template.php(11) : eval()'d code on line 1
Welcome to my site.
Parse error: parse error, unexpected '<' in ../Docs/test/php/template.php(14) : eval()'d code on line 1
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

oops.. didn't notice you were eval'ing it..

Code: Select all

$Content = "echo "{$Template['content']}";";
my bad.
mshirley
Forum Newbie
Posts: 3
Joined: Thu May 20, 2004 6:18 pm

Post by mshirley »

WAHOO! IT WOKS NOW!

Thanks for the help!

I've only used the eval a few time, so this instance tossed me for a loop, or rather an error! :lol:
Post Reply