I'm developing a php web application using Dreamweaver CS5. When I create a new page, Dreamweaver automatically adds the following code

<!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>Untitled Document</title>
</head>
<body>
</body>
</html>

What I'm trying to do is to take this code and put it in a file, say 'head.php', and then I want to import this head.php file to all the pages I create using

<?php include("head.php"); ?>

If I do this, it means that I'm gonna have the same page title for every page because the code <title>Untitled Document</title> is included in head.php.

So is there a way for me to send the page title via a variable from the new pages I create and then set it on the head.php document. So if I had a customer.php page, it would look something like this.

$pageTitle = "Customer List";
<?php include("head.php"); ?>

Then pass $pageTitle to the the head.php

有帮助吗?

解决方案

Inside your header.php

function header($title)
{
 echo '<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>.$title.</title>
 </head>';
}

then you include your head.php

< ?php include("head.php"); ?>

and finally, instead the < head >< /head > tags in your main file your call the function to create the title.

<?php header('This is my Awesome Page'); ?>

Or still

<?php 
  $pageTitle = "My Title";
  header($pageTitle); 
?>

This should work for you.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top