2

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'>&larr; 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=utf-8" />
<title><?php echo $pagetitle; ?></title>
</head>
<body>
<br />
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
Hugo
  • 21
  • 1
  • 5
  • 1
    @Hugo: Are you including `header.php` *before* you get the page title...? – netcoder Feb 12 '11 at 07:44
  • Yes, I am. But I need to get title from the database first (in the while loop). When I put the pagetitle variable inside the while loop nothing happens.. That's my problem. – Hugo Feb 12 '11 at 07:45
  • Start debugging e.g Are you sure there are results fetched from the Database? – Andreas Feb 12 '11 at 08:06
  • Yeah, there are results from the database, they display on the website. – Hugo Feb 12 '11 at 08:12
  • sugguestion: place all title related code (fetching from DB, setting the appropriate variable at the top of your header.php file) – Andreas Feb 12 '11 at 08:14
  • Well, of course you need to do the query BEFORE printing the header, or the variable won't be set...You query the DB at the top of the page, print the title there, and the rest in page body. Or you can go with 2 queries, one for title only a the top of the page, the other when and where and how you want. – Damien Pirsy Feb 12 '11 at 08:15

3 Answers3

1

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

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

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>
0

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.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405