Hello, I got a problem, namely I'm trying to do a webpage by using templates, I was doing really well until it reached to point where I had to take some data form mysql database and loop it to template, I checked some tutorials and tried to work it out bu myself but it didn't happen and I'm still struggling with that issue.
Namely, my code (functions works fine when I am printing a template without mysql data):
<?php
$hostname="xxx";
$username="xxx";
$password="xxx";
$database="xxx";
function read_template($filename)
{
$source = join("",file($filename));
if (strlen($source) == 0)
{
die("Templatefaili ei õnnestud avada!");
};
return $source;
};
function parse_template($source,$vars)
{
if (is_array($vars))
{
foreach($vars as $key => $value)
{
$tag = "{VAR:" . $key . "}";
$source = str_replace($tag,$value,$source);
};
};
return $source;
}
$db = mysql_connect($hostname,$username,$password);
mysql_select_db($database,$db);
$sqlmd = "SELECT * FROM uudised ORDER BY id DESC";
$result = mysql_query($sqlmd) or die ("Uudiste kuvamine ebaõnnestus ".mysql_error());
$results = array();
$i = "1";
while($row = mysql_fetch_array($result))
{
$tmp = array(
'lisaja' => $row['lisaja'],
'aeg'=> $row['aeg']
);
$results[$i++] = $tmp;
}
$main_template = "esileht.tpl";
$frame_tpl = read_template($main_template);
$vars = array("results" => $results);
$site_html = parse_template($frame_tpl, $vars);
print $site_html;
?>
And that is my template "esileht.tpl" where I try to loop mysql data:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Katsetused :D</TITLE>
</HEAD>
<BODY>
{section name=news loop=$results}
{VAR:results[news].lisaja}<BR>
{VAR:results[news].aeg}<BR>
{/section}
</BODY>
</HTML>
When I try to print my mysql data, the browser prints only empty tags, no data, this is that browser prints...
{section name=news loop=$results} {VAR:results[news].lisaja}
{VAR:results[news].aeg}
{/section}
...and nothing else. I have no idea what to do, I have tried many different options but nothing seems to work. I hope you can help me to seolve my problem.
Good luck,
HPH