views:

90

answers:

5

We include a header.php file across all the pages on our site. So we could either place a single title in the header.php file which would be applied to the entire site, or have a custom header within each page to be more descriptive.

The problem is that in doing that, the title would be outside the head tags (which remain in the header.php file) and marked as invalid.

Is there some kind of function we can use to define a variable ($pageTitle) within the page, that will be displayed in head tag?

Thanks.

+1  A: 

Ummm.....

<?php 
$pageTitle = "Test";
include('header.php');
?>

EDIT

Then in your header.php

<head>
    <title><?php echo $pageTitle; ?> </title>
</head>
Brad F Jacobs
I should have clarified, the header.php file contains neccesary functions to generate the page (connecting to database, etc.). So for example, when we have a story, we have to load the header.php file first in order to make the connection and pull the story details. Then I'd like to have the title be the news entry title. That's more specific to the problem.
scatteredbomb
Yes, using a global page title variable will work and you just have to make sure as in answer you declare its value before using it in the header. You can also have a default value and this way in your header.php do: if(empty($pageTitle)){//set title to default value//}else{//set title to $pageTitle}
Chris
@scatteredbomb well you just have a bad design and have to rebuild it. Divide your header into 2 parts.
Col. Shrapnel
A: 

Your PHP page starts would be:

<?php
$title="My custom title...";

require("header.php");
// ...
?>

And your header.php would be:

<?php
//...
echo "<head>
<title>".$title."</title>";
//...
?>

You could even default to a title in header.php:

<?php
//...
echo "<head>
<title>".($title==""?"Default title":$title)."</title>";
//...
?>
Rudu
+1  A: 

It looks like you want a dynamic title on some pages?

<?php
$defaultPageTitle='Default Title'; //Default title
include('header.php');
?>

<?php
/* You would define $newPageTitle here if necessary
 (i.e. use $_SERVER['REQUEST_URI'] to get the URL
 and check a database for the $newPageTitle */
?>
<head>
<?php
if(isset($newPageTitle)){echo '<title>'.$newPageTitle.'</title>';}
else{echo '<title>'.$defaultPageTitle.'</title>';}
?>
</head>
Matt
The problem being that we already use the head tags in header.php. We have to setup meta tags, stylesheets, etc. and that is done in that header. These stylesheets and other includes are used to style the actual header (tabs/links) so we can't wait to define them later in the page after we've pulled out the necessary data to create the title.
scatteredbomb
@scatteredbomb Yes, you aimed the problem exactly! There should be no output before you pull out all the necessary data to create the whole page!
Col. Shrapnel
Oops, I missed that part when I first read the question! :)
Matt
+3  A: 

Actually it should be this way

news.php:

<?
include "config.php"; //connect to database HERE.
$data = getdbdata("SELECT * FROM news where id = %d",$_GET['id']);
$page_title = $data['title'];
$body = nl2br($data['body']);

$tpl_file = "tpl.news.php";
include "template.php";
?>

template.php:

<html>
<head>
<title><?=$page_title?></title>
</head>
<body>
<? include $tpl_file ?>
</body>

tpl.news.php

<h1><?=$page_title?></h1>
<?=$body?>

plain and simple

Col. Shrapnel
Everything you always wanted to know including the database schema! And converting new lines in the body! You rock!
zaf
hehe I love such comments. Though the idea of using templates is not that obvious.
Col. Shrapnel
Thanks for the reply and the comments. I guess I did just have bad design that leads to part of the page being pulled before the rest. I'll have to figure out how to implement your basic idea into our site. Thanks again.
scatteredbomb
A: 

The way I see it, you can still do all this work in your header:

<?php
include(...your config/connect file...);
mysql_query(... get page variables ...);
$pageTitle = stripslashes($titlefromDB);
?>
<html><head><title><?php echo $pageTitle; ?></head>

Thus concludes you header.php. Now include this on every page you want to use it, and follow with your <body></body></html>.

Just one idea, but anyway you go around this, you're going to have to connect to your DB first, check if page exists, if so set title as variable, then begin constructing html.

Bryan
it cannot be included into every page. just because every page needs **different** variables. It's template should be included into main page, not contrary
Col. Shrapnel
How so? I currently get my dynamic requests from the url ($_GET['id']) and plug them right into my mysql_query (with some sanitization...). Now every page on my site has different needs, so each folder has an index with different header php, and I include the meta.php in each.
Bryan