动态页面标题

时间:2011-02-12 07:31:19

标签: php

我想为页面创建一个动态标题,我知道如何做,但我似乎无法使它工作。

如果我这样做有效;

<head><title><?php echo $pagetitle; ?></head>
<?php $pagetitle = "Homepage"; ?>

但此刻我正在使用while循环从MySQL表中提取数据;

while ($row = mysql_fetch_array($query)) {

我需要将$ pagetitle变量放在while循环中,因为它是符合的;

$pagetitle1 = $row['title'];
$pagetitle = "$pagetitle1 ~website.com";

以下是涉及的页面;

index.php(我需要放入pagetitle变量)
    

$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 />

3 个答案:

答案 0 :(得分:1)

  

但此刻我正在使用while循环从MySQL表中提取数据;

所以,你做错了。
您可能已经注意到,您的主要问题是数据库循环。要解决这个问题,您不应该直接在循环中输出数据,而是将其全部收集到某个变量中以备将来使用。然后才开始输出。

在我之前的回答中,我写了关于如何使用简单PHP脚本的模板的完整工作示例。它完全适合您的情况:Using Template on PHP

答案 1 :(得分:1)

只要您在使用变量时查询数据库中的页面内容,它应该可以正常工作,例如:

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

答案 2 :(得分:0)

如果您愿意,我建议您使用模板引擎(如PHPTAL或XSLT),而不是使用页眉和页脚文件进行打印。

无论如何,你应该做的是:

setup.php:执行所有查询以及为文件构建设置的所有内容。没有打印 header.php:安装后包含,它将打印正确的标题。 page.php:页面内容。

建议在构建完毕后立即回显所有页面内容(例如将header.php的内容存储到变量中),因为这样更容易管理。

相关问题