473,372 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

How to dynamically change page title using php and require_once?

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
6 38303
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: TheKeith | last post by:
I don't know much about javascript, so take it easy on me. Is there a way to dynamically change a CSS layers dimensions on the fly. Here is what I'm doing. I have a bunch of thumbnails that when...
4
by: Bruce W...1 | last post by:
To dynamically change something on an ASP.NET web page a literal control (and others) can be used. But how can a non-visual element, like a page meta tag, be changed dynamically? Thanks for...
5
by: Maxim Izbrodin | last post by:
Hello For displaying page titles for my ASP.NET applications I use the following technique <title><%=BannerModule.PageTitle%></title where BannerModule.PageTitle is a public field of my user...
5
by: SStory | last post by:
I would like to make the title of the page change according to invoice # how can I do this? didn't see a field on the page property in the code behind file. Thanks, Shane
14
by: Paul | last post by:
I want to set the page title and/or a form hidden field programatically through ASP.Net. I do not want to use something like... <% sTitle ="My Title" %> <html><title><%=sTitle%></title>..... ...
5
by: Christian Ista | last post by:
Hello, I have a problem to change title page at runtime. In the page ASPX : <title runat="server" id="pageTitle"></title> In code behind : public partial class _Default : System.Web.UI.Page
3
by: Laith Zraikat | last post by:
I need to set the page title for a content page in a master page, and do that using local reources so that I can have my page title in different languages. I can do that easily using global...
3
by: synergy_711 | last post by:
I feel like this should be fairly easy but I have been struggling with this for sometime. I have not been able to find someone who's had the same problem as mine yet. ...
2
by: Stuartsblog | last post by:
Is it possible to return the Page title in php ? I can return the end of the url with $_SERVER however it is the "Page Title" that I require. Thanks
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.