PHP template
I've a question about setting up templates with php and tpl files. I've a page called test.php and inside I connect to a mysql db. Now I've one part of my php code that uses one tpl file. Which is a table that lists all the fields names that I'm calling from the db. Then the next part I loop through a list of all the info. Now my question is how do I combine the 2 tpl files? If I do then I get repeats from the news.tpl file. You can view what I've so fare. http://www.finalphase.org/php/test.php
##################### PHP page ########################
FINAL PHASE PHP NEWS PRO
<?php
$connection = mysql_connect("**************","*********","****************")
or die("Couldn't execute query: " . mysql_error());
$db = mysql_select_db("blah", $connection)
or die("Couldn't execute query: " . mysql_error());
$sql = "SELECT id, title, author, date, body FROM n_main ORDER BY id DESC";
$sql_result = mysql_query($sql, $connection)
or die("Couldn't execute query: " . mysql_error());
include "inc/template.inc";
// Sets the first template news.tpl
$t = new Template("temp/");
$t->set_file("Handle","news.tpl");
$t->set_var("some_color",$my_color);
$t->parse("Output","Handle");
$t->p("Output");
while ($row = mysql_fetch_array ($sql_result)) {
$title = $row["title"];
$author = $row["author"];
$date = $row["date"];
$body = $row["body"];
// Sets the 2nd template repeat.tpl
$t->set_file("WholeHandle","repeat.tpl");
$t->set_var("title",$title);
$t->set_var("author",$author);
$t->set_var("date",$date);
$t->set_var("body",$body);
$t->parse("FinalOutput","WholeHandle");
$t->p("FinalOutput");
}
mysql_free_result($sql_result);
mysql_close($connection);
?>
############################## news.tpl ######################
Title
Author
Date
Body
############################## Repeat.tpl ####################
{title}
{author}
{date}
{body}
Mark Hensler posted this at 23:37 — 15th July 2002.
He has: 4,048 posts
Joined: Aug 2000
Repeat.tmp shouldn't include the table tags:
{title}
{author}
{date}
{body}
venom posted this at 00:01 — 16th July 2002.
They have: 34 posts
Joined: Feb 2002
If I do that then the code will look like this..
Title
Author
Date
Body
{title}
{author}
{date}
{body}
Now I may be wrong but that's not proper html coding or is it?
www.finalphase.org
ROB posted this at 00:16 — 16th July 2002.
They have: 447 posts
Joined: Oct 1999
news.tmpl should have the table tags, not the repeating template
Mark Hensler posted this at 03:06 — 16th July 2002.
He has: 4,048 posts
Joined: Aug 2000
Re-design $t->p() to return a string rather than print.... now you can nest template files.
<?php
<html>
<head>
<title>FINAL PHASE PHP NEWS PRO</title>
</head>
<body>
$connection = mysql_connect(\"**************\",\"*********\",\"****************\")
or die(\"Couldn't execute query: \" . mysql_error());
$db = mysql_select_db(\"blah\", $connection)
or die(\"Couldn't execute query: \" . mysql_error());
$sql = \"SELECT id, title, author, date, body FROM n_main ORDER BY id DESC\";
$sql_result = mysql_query($sql, $connection)
or die(\"Couldn't execute query: \" . mysql_error());
include \"inc/template.inc\";
$t = new Template(\"temp/\");
$detail_lines = \"\";
while ($row = mysql_fetch_array ($sql_result)) {
$title = $row[\"title\"];
$author = $row[\"author\"];
$date = $row[\"date\"];
$body = $row[\"body\"];
// Sets the 2nd template repeat.tpl
$t->set_file(\"WholeHandle\", \"repeat.tpl\");
$t->set_var(\"title\", $title);
$t->set_var(\"author\", $author);
$t->set_var(\"date\", $date);
$t->set_var(\"body\", $body);
$t->parse(\"FinalOutput\", \"WholeHandle\");
$detail_lines .= $t->p(\"FinalOutput\");
}
// Sets the first template news.tpl
$t->set_file(\"Handle\", \"news.tpl\");
$t->set_var(\"some_color\", $my_color);
$t->set_var(\"detail_lines\", $detail_lines);
$t->parse(\"Output\", \"Handle\");
echo $t->p(\"Output\");
mysql_free_result($sql_result);
mysql_close($connection);
</body>
</html>
?>
############################## news.tpl ######################
<table width="750">
<TR>
<TH align="left" valign="top">Title</TH>
<TH align="left" valign="top">Author</TH>
<TH align="left" valign="top">Date</TH>
<TH align="left" valign="top">Body</TH>
</TR>
{detail_lines}
</table>
############################## Repeat.tpl ####################
<TR>
<th align="left" valign="top">{title}</th>
<TH align="left" valign="top">{author}</TH>
<TH align="left" valign="top">{date}</TH>
<TH align="left" valign="top">{body}</TH>
</TR>
Mark Hensler
If there is no answer on Google, then there is no question.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.