By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,854 Members | 876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,854 IT Pros & Developers. It's quick & easy.

How to dynamically change page title using php and require_once?

P: n/a
This is an elementary question, but I've not been able to find the
answer, so here goes:

I am developing a site using php. I have the html header information
in a file that I include in all the pages using the require_once
function.

That is, each page includes the line

<?require_once('PageStart.php')?>

The PageStart.php file essentially defines the header. Omitting the
boring stuff, it's laid out like so:

<html>
<head>
<title>The Title of the Page</title>
<?require_once('styles/PageStyles.css')?>
</head>

<boring stuff snipped>

This works great. The only complaint I have is that there are LOTS of
pages on the site, and using this approach means that every page is
titled "The Title of the Page" When scanning my browser history, I
could have surfed to 30 different pages in the site and they all have
the same title. Makes it hard to know which page I'd like to go back
to.

I'd like to have a way to pass a (sometimes) dynamically generated
page title to the PageStart.php file. I say sometimes because certain
pages (the Main one for example) will always have the same title, so
that could be passed statically. There is another series of pages in
which the same php file accesses a MySQL database to display
information about various family members, and I'd like that title to
be generated dynamically (for example, "All about Mary" or "All about
Ted" depending on the person being described in the page).

Now I KNOW that with php there's a way to do this. I just can't find
it.
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I noticed that Message-ID: <2o********************************@4ax.com>
from the wonderer contained the following:

<?require_once('PageStart.php')?>

The PageStart.php file essentially defines the header. Omitting the
boring stuff, it's laid out like so:

<html>
<head>
<title>The Title of the Page</title>
<?require_once('styles/PageStyles.css')?>
</head>

<boring stuff snipped>


Make it a function. Make it so that if you don't pass a value you get
the default page title and if you do then it uses that title. The value
can be generated from a query for your All about Mary pages.

<?php
function header($title){
if(!$title){
$title="default page title";
}
?>
<html>
<head>
<title><?php print $title; ?></title>
<?require_once('styles/PageStyles.css')?>
</head>

<?php } ?>

Then in your page

<?require_once('PageStart.php');
$title="My new Page";
header($title);
?>

or for the default

<?require_once('PageStart.php');
header($title);
?>


--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2

P: n/a
On Fri, 17 Oct 2003 18:11:31 +0100, Geoff Berrow
<bl@ckdog.co.uk.the.cat> wrote:
I noticed that Message-ID: <2o********************************@4ax.com>
from the wonderer contained the following:

<?require_once('PageStart.php')?>

The PageStart.php file essentially defines the header. Omitting the
boring stuff, it's laid out like so:

<html>
<head>
<title>The Title of the Page</title>
<?require_once('styles/PageStyles.css')?>
</head>

<boring stuff snipped>


Make it a function. Make it so that if you don't pass a value you get
the default page title and if you do then it uses that title. The value
can be generated from a query for your All about Mary pages.

<?php
function header($title){
if(!$title){
$title="default page title";
}
?>
<html>
<head>
<title><?php print $title; ?></title>
<?require_once('styles/PageStyles.css')?>
</head>

<?php } ?>

Then in your page

<?require_once('PageStart.php');
$title="My new Page";
header($title);
?>

or for the default

<?require_once('PageStart.php');
header($title);
?>

Thanks Geoff. I'm not sure though if I understand this.

I currently have the header information, including title, in
PageStart.php.

Are you saying to incorporate the header function into the
PageStart.php code, or make the function a separate file, or put the
function into any php file that would need it? Should I remove the
title declaration from PageStart.php altogether?

I'm not sure how this would work:

<?require_once('PageStart.php');
$title="My new Page";
header($title);
?>

or for the default

<?require_once('PageStart.php');
header($title);

Since PageStart.php already has <title> in it, wouldn't the subsequent
header($title) function either be ignored or cause a problem?
Jul 17 '05 #3

P: n/a
I noticed that Message-ID: <c5********************************@4ax.com>
from the wonderer contained the following:
Thanks Geoff. I'm not sure though if I understand this.

I currently have the header information, including title, in
PageStart.php.

Are you saying to incorporate the header function into the
PageStart.php code, or make the function a separate file, or put the
function into any php file that would need it? Should I remove the
title declaration from PageStart.php altogether?

I'm not sure how this would work:
First of all let me apologise. The function cannot be called header()
because that already exists in PHP (duh!) So call it head()
<?require_once('PageStart.php');
$title="My new Page";
header($title);
?>

or for the default

<?require_once('PageStart.php');
header($title);

Since PageStart.php already has <title> in it, wouldn't the subsequent
header($title) function either be ignored or cause a problem?


The variable $title is what is passed to the function. If it isn't set
then the default title is used. Here is my (corrected) code:

PageStart.php (it's actually the full page here, but could just be the
header naturally)

<?php
function head($title){
if(!$title){
$title="Default page title";
}
?>
<html>
<head>
<title><?php print $title; ?></title>

</head>
<body>
hello world
</body>
</html>

<?php } ?>

And pages that use it
p1.php

<?require_once('PageStart.php');
head("My new Page");
?>

p2.php

<?require_once('PageStart.php');
head($title);
?>

p3.php
<?require_once('PageStart.php');
$title="something";
head($title);
?>

It all works. I even tried

p4.php

<?require_once('PageStart.php');
$title="something";
head(5+7/3);
?>

:-)

See:
www.ckdog.co.uk/php/test
--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #4

P: n/a
On Fri, 17 Oct 2003 19:14:01 +0100, Geoff Berrow
<bl@ckdog.co.uk.the.cat> wrote:
I noticed that Message-ID: <c5********************************@4ax.com>
from the wonderer contained the following:
Thanks Geoff. I'm not sure though if I understand this.

I currently have the header information, including title, in
PageStart.php.

Are you saying to incorporate the header function into the
PageStart.php code, or make the function a separate file, or put the
function into any php file that would need it? Should I remove the
title declaration from PageStart.php altogether?

I'm not sure how this would work:


First of all let me apologise. The function cannot be called header()
because that already exists in PHP (duh!) So call it head()

<?require_once('PageStart.php');
$title="My new Page";
header($title);
?>

or for the default

<?require_once('PageStart.php');
header($title);

Since PageStart.php already has <title> in it, wouldn't the subsequent
header($title) function either be ignored or cause a problem?


The variable $title is what is passed to the function. If it isn't set
then the default title is used. Here is my (corrected) code:

PageStart.php (it's actually the full page here, but could just be the
header naturally)

<?php
function head($title){
if(!$title){
$title="Default page title";
}
?>
<html>
<head>
<title><?php print $title; ?></title>

</head>
<body>
hello world
</body>
</html>

<?php } ?>

And pages that use it
p1.php

<?require_once('PageStart.php');
head("My new Page");
?>

p2.php

<?require_once('PageStart.php');
head($title);
?>

p3.php
<?require_once('PageStart.php');
$title="something";
head($title);
?>

It all works. I even tried

p4.php

<?require_once('PageStart.php');
$title="something";
head(5+7/3);
?>

:-)

See:
www.ckdog.co.uk/php/test


Okay, I reread your post and I went to the /php/test url above. So I
see that it works. But in trying to implement it, I'm failing. Maybe
if I could see the source of your pX.php and PageStart.php files?

Here is the start of my PageStart.php file (note that I changed the
name of your function head() to make_title() just so as not to confuse
the function head() with the <head> tag):

<?php
function make_title($title)
{
if(!$title)
{
$title="Page Default Title";
}
?>

<?php } ?>

<html>
<head>
<title><?php print $title; ?></title>
<?require_once('styles/BlueAndGold.css')?>
</head>

Now here is the first line of my index.php page:

<?require_once('PageStart1017.php', $title='My Web Site: Main
Page')?>

That line triggers a parse error. Since PageStart.php is included in
virtually every page on the web site, I don't know how to "let it
know" what page title is being used.

I appreciate your help on this. I am a php novice (obviously!) and
much of what I do consists of copying stuff I understand, then
gradually modifying it to suit my needs, with frequent visits to
php.net to check what I'm doing.
Jul 17 '05 #5

P: n/a
I noticed that Message-ID: <59********************************@4ax.com>
from the wonderer contained the following:
Okay, I reread your post and I went to the /php/test url above. So I
see that it works. But in trying to implement it, I'm failing. Maybe
if I could see the source of your pX.php and PageStart.php files?
It's all there. I quoted the entire source.
Here is the start of my PageStart.php file (note that I changed the
name of your function head() to make_title() just so as not to confuse
the function head() with the <head> tag):

<?php
function make_title($title)
{
if(!$title)
{
$title="Page Default Title";
}
?>

<?php } ?>

<html>
<head>
<title><?php print $title; ?></title>
<?require_once('styles/BlueAndGold.css')?>
</head>

Now here is the first line of my index.php page:

<?require_once('PageStart1017.php', $title='My Web Site: Main
Page')?>

That line triggers a parse error. Since PageStart.php is included in
virtually every page on the web site, I don't know how to "let it
know" what page title is being used.
This is the bit where you 'call' the function. Basically what you are
doing is saying "run the bit of code called 'make_title' and pass the
parameter $title to it".

So on each page you have:

<?require_once('PageStart1017.php');
$title="My Web Site: MainPage";
make_title($title);
?>
Changing the $title variable to suit, of course.

For the default use:

$title="";

If $title doesn't contain anything, the if() makes the title the page
default title.

Obviously you can then get $title from anywhere, a database query, if
you are that advanced.
I appreciate your help on this. I am a php novice (obviously!) and
much of what I do consists of copying stuff I understand, then
gradually modifying it to suit my needs, with frequent visits to
php.net to check what I'm doing.


I'm still very much a novice myself and I'm glad to help. I get a lot
of help from other people - it's only fair to give it if you can, plus
it's a great way to learn.
--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #6

P: n/a
Oops Geoff... I never replied again the other day. I was overthinking
things. Your simple suggestions worked perfectly. Thanks again.
On Fri, 17 Oct 2003 21:57:51 +0100, Geoff Berrow
<bl@ckdog.co.uk.the.cat> wrote:
I noticed that Message-ID: <59********************************@4ax.com>
from the wonderer contained the following:
Okay, I reread your post and I went to the /php/test url above. So I
see that it works. But in trying to implement it, I'm failing. Maybe
if I could see the source of your pX.php and PageStart.php files?


It's all there. I quoted the entire source.

Here is the start of my PageStart.php file (note that I changed the
name of your function head() to make_title() just so as not to confuse
the function head() with the <head> tag):

<?php
function make_title($title)
{
if(!$title)
{
$title="Page Default Title";
}
?>

<?php } ?>

<html>
<head>
<title><?php print $title; ?></title>
<?require_once('styles/BlueAndGold.css')?>
</head>

Now here is the first line of my index.php page:

<?require_once('PageStart1017.php', $title='My Web Site: Main
Page')?>

That line triggers a parse error. Since PageStart.php is included in
virtually every page on the web site, I don't know how to "let it
know" what page title is being used.


This is the bit where you 'call' the function. Basically what you are
doing is saying "run the bit of code called 'make_title' and pass the
parameter $title to it".

So on each page you have:

<?require_once('PageStart1017.php');
$title="My Web Site: MainPage";
make_title($title);
?>
Changing the $title variable to suit, of course.

For the default use:

$title="";

If $title doesn't contain anything, the if() makes the title the page
default title.

Obviously you can then get $title from anywhere, a database query, if
you are that advanced.

I appreciate your help on this. I am a php novice (obviously!) and
much of what I do consists of copying stuff I understand, then
gradually modifying it to suit my needs, with frequent visits to
php.net to check what I'm doing.


I'm still very much a novice myself and I'm glad to help. I get a lot
of help from other people - it's only fair to give it if you can, plus
it's a great way to learn.


Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.