动态更改标题和描述
Change title and description dynamically
我想从需要 MySQL 查询的页面生成动态标题和描述:
index.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>
那么比如我应该调用页面,例如:
members.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放在标题 标签
提前致谢
正如我们从给定的代码中看到的,您可以在下面执行此操作。
如果您真的不想重写整个代码。
members.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>
不过还是建议大家一开始就准备好服务器端的变量,这样就不会遇到这种问题了。
我想从需要 MySQL 查询的页面生成动态标题和描述:
index.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>
那么比如我应该调用页面,例如:
members.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放在标题 标签
提前致谢
正如我们从给定的代码中看到的,您可以在下面执行此操作。 如果您真的不想重写整个代码。
members.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>
不过还是建议大家一开始就准备好服务器端的变量,这样就不会遇到这种问题了。