Dynamic Title of a page
I want to create a dynamic title for pages, I understand how to do it but I can't seem to make it work.
If I do this it works;
<head><title><?php echo $pagetitle; ?></head>
<?php $pagetitle = "Homepage"; ?>
But at the moment I'm pulling data from a MySQL table using a while loop;
while ($row = mysql_fetch_array($query)) {
And I need the $pagetitle variable to be within the while loop because it's something along the lines of;
$pagetitle1 = $row['title'];
$pagetitle = "$pagetitle1 ~website.com";
Here are the pages involved;
index.php (where I need to put in the pagetitle variable)
$title = $row['title'];
$author = $row['author'];
$email = $row['author_email'];
$cat = $row['category'];
$content = $row['content'];
$date = $row['date'];
$id = $row['id'];
echo "<center>
<table border='0' width='100%' cellspacing='10'>
<tr>
<td width='20%' valign='top'><div class='title'><a href=\"?id=$id\">$title</a></div>
<div class='info'><i>$date</i><br />
By <a href='mailto:$email'>$author</a><br />
$cat</div>
</td>
<td width='80%' valign='top'>";
echo nl2br($content);
echo "</td>
</tr>
</table>
<hr />
</center>
";
}
}
else
$query = mysql_query("SELECT * FROM blogdata WHERE id = '" . $_GET['id'] . "'");
while ($row = mysql_fetch_array($query)) {
$title = $row['title'];
$author = $row['author'];
$email = $row['author_email'];
$cat = $row['category'];
$content = $row['content'];
$date = $row['date'];
echo "<center>
<table border='0' width='100%' cellspacing='10'>
<tr>
<td width='20%' valign='top'><div class='title'><a href=''>$title</a></div>
<div class='info'><i>$date</i><br />
By <a href='mailto:$email'>$author</a><br />
$cat</div>
</td>
<td width='80%' valign='top'>";
echo nl2br($content);
echo "</td>
</tr>
</table>
<hr />
<a href='index.php'>← Rewind.</a>
</center>
";
}
require("footer.php");
?>
header.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ut开发者_如何学运维f-8" />
<title><?php echo $pagetitle; ?></title>
</head>
<body>
<br />
But at the moment I'm pulling data from a MySQL table using a while loop;
So, you're doing it wrong way.
As you may be already noted, your main problem is a database loop. To solve this problem you just shouldn't output the data directly within the loop but rather gather it all in some variable for the future use. And only then start any output.
In my earlier answer I wrote full working example on how to use templates with simple PHP scripts. It fits your case exactly: Using Template on PHP
As long as you are querying the database for your page's content BEFORE using the variables it should work just fine, for example:
<?php
$query = mysql_query(" SELECT * FROM posts WHERE id = '$id' ");
while ($row = mysql_fetch_array($query))
{
$page_title = $row['page_title'];
$page_content = $row['page_content'];
}
?>
<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body>
<?php echo $page_content; ?>
</body>
</html>
I would advise that you use a templating engine such as PHPTAL, or XSLT if you prefer, rather than having header and footer files to do your printing.
At any rate, what you should do is something like:
setup.php : does all queries and everything to build the set up for the file. Does no printing header.php : included after setup, it will print the correct title. page.php : page content.
It is also advisable to echo all of the page content at once at the end once it's built (e.g. store the content of header.php into a variable) as this is easier to manage.
精彩评论