我想从需要MySQL查询的页面生成动态标题和描述:
索引.php
<html>
<head>
<?php
switch($page):
case 'home':
$body = 'home.php';
$title = 'Home';
break;
case 'contact':
$body = 'contact.php';
$title = 'Contact';
break;
case 'members':
$body = 'members.php';
$title = 'I should put here the member profile';
break;
/*
* More pages
*/
endswitch;
?>
<title><?php echo $title; ?></title>
</head>
<body>
<?php
require_once $body;
?>
</body>
然后,例如我应该调用页面,例如:
成员.php页
<?php
$sql = "SELECT * FROM members WHERE mem_id = :mem_id";
/*
* More codes
*/
foreach($res as $f):
$mem_name = $f['mem_name'];
endforeach;
$newTitle = 'Profile of '.$meme_name;
?>
那么如何将MySQL 查询生成的$newTitle放在标题标签中
提前致谢
正如我们从给定的代码中看到的,您可以在下面执行。 如果你真的不想重写整个代码。
成员.php
<?php
$sql = "SELECT * FROM members WHERE mem_id = :mem_id";
/*
* More codes
*/
foreach($res as $f):
$mem_name = $f['mem_name'];
endforeach;
$newTitle = 'Profile of '.$meme_name;
require_once $body;
?>
// put this at the buttom of your body
<script>
document.title="<?php echo $newTitle;?>"
</script>
但是仍然建议您在开始时准备服务器端变量,这样您就不会遇到此类问题。