Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Closed Thread
Dynamic title using PHP
Old 01-09-2009, 02:02 AM Dynamic title using PHP
Average Talker

Posts: 19
Trades: 0
I have a webpage with a title in the content of every pages (news, story, about, pictures) and it's in text.

Now my problem is that I want to retrieve these text/title using php echo into <title> but it's not showing up.

I've read on many pages that the in order for the echo in the <title> to work, I have to put the variable before the echo but I can't do this because the variable is where my contents are.

example:

Quote:
<head>
<title><?php echo $title;?></title>
</head>
<body>


<?php $title="news";?>

<?php echo $title;?>
</body>


The bolded part is in a file that all pages uses through include. So I cannot put the variable in there because then I would have to put a variable for the other pages in there too.

Basically I just want to work with one file to change two titles, I could include a php $title="news" code in a file before the header but then there will a file like that for every page.

All in all, does anyone know how I can get the $title from the content of my page into the <title>?

I read somewhere that you can use the function but I'm not familiar with function myself, so if anyone can guide, it would be appreciated.
rogerchin85 is offline
View Public Profile
 
 
Register now for full access!
Old 01-09-2009, 03:29 AM Re: Dynamic title using PHP
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
What you're suggesting can only be done with JavaScript, but I'd say it's not a comfortable solution.

It really is better to get the variable above the echo.
What I do when making pages like this, is put all the PHP before the first HTML element, and store all the loading and including in variables.
Then in the HTML I simply echo them.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
View Public Profile
 
Old 01-10-2009, 05:50 PM Re: Dynamic title using PHP
PeachyJuice's Avatar
Super Talker

Posts: 116
Name: Michele T.
Location: Ny, Ny
Trades: 1
If you switch them it will work. That's how I title my pages using header includes. Just do it like this, on every page:
Code:
<?php
$title = 'News';
include('header.php');

?>
And on the header file...
Code:
<?php 
echo '<title>';
if(isset($title)) echo $title;
else echo 'Website'; //a default in case you forge to put in $title
echo '</title>';

?>
It's a simple solution =)
__________________
Freelance web+graphic designer and PHP developer.

Please login or register to view this content. Registration is FREE
PeachyJuice is offline
View Public Profile
 
Old 01-10-2009, 06:09 PM Re: Dynamic title using PHP
Decaf's Avatar
Ultra Talker

Posts: 489
Name: Adam
Trades: 0
Or gather the page's name, remove the file extension and print that.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Decaf is offline
View Public Profile Visit Decaf's homepage!
 
Old 01-10-2009, 08:29 PM Re: Dynamic title using PHP
NullPointer's Avatar
Will Code for Food

Posts: 2,784
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by Decaf View Post
Or gather the page's name, remove the file extension and print that.
Like the man said:
PHP Code:
$fileName $_SERVER['PHP_SELF'];
$fileName explode('.'$fileName);
$pageName $fileName[0];
$pageName str_replace('/'''$pageName); 
Additionally if you do not want your title to be the name of the page just use a switch.

PHP Code:
$title;
switch(
$pageName)
{
    case 
'x':
        
$title 'y';
        break;
    case 
'a':
        
$title 'b';
        break;

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
View Public Profile Visit NullPointer's homepage!
 
Old 01-10-2009, 10:12 PM Re: Dynamic title using PHP
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
Actually, I like the solution PeachyJuice gave. It has the advantage of allowing you to use the header file from any page without having to add it to the header file. Usually when programming, less intelligent is better!
__________________
Will Anderson

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
anderswc is offline
View Public Profile Visit anderswc's homepage!
 
Old 01-10-2009, 10:29 PM Re: Dynamic title using PHP
Decaf's Avatar
Ultra Talker

Posts: 489
Name: Adam
Trades: 0
What about a mix of both?

title.php
PHP Code:
$siteName 'Example';

$fileName $_SERVER['PHP_SELF'];
$fileName explode('.'$fileName);
$pageName $fileName[0];
$pageName str_replace('/'''$pageName);

print 
ucfirst($pageName) . " | $siteName"
Produces "{PAGE} | Example" aka.

Home | Google

Youwill only have to edit the $siteName variable, otherwise its set. All you need to do is include the script on your pages.

HTML Code:
<html>
<head>
<title><?php include("title.php"); ?></title>
...
</head>
<body>
...
</body>
</html>
It can't get any easier.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


Last edited by Decaf; 01-10-2009 at 10:31 PM..
Decaf is offline
View Public Profile Visit Decaf's homepage!
 
Old 01-11-2009, 06:42 AM Re: Dynamic title using PHP
Novice Talker

Posts: 15
Name: Jamal
Trades: 0
Quote:
Originally Posted by PeachyJuice View Post
If you switch them it will work. That's how I title my pages using header includes. Just do it like this, on every page:
Code:
<?php
$title = 'News';
include('header.php');

?>
And on the header file...
Code:
<?php 
echo '<title>';
if(isset($title)) echo $title;
else echo 'Website'; //a default in case you forge to put in $title
echo '</title>';

?>
It's a simple solution =)

thanks is the simple and the best solution
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
scriptbazaar is offline
View Public Profile
 
Old 01-11-2009, 09:38 PM Re: Dynamic title using PHP
Decaf's Avatar
Ultra Talker

Posts: 489
Name: Adam
Trades: 0
Here is a dynamic version for a whole directory, all you have to do is change one variable, (plus the static title text).

PHP Code:
<?php
# Dynamic Title Text
# This also has page loading time because its from another project.

# Gather the starting time
$startTime explode(" ",microtime());

# Set Vars
$directory "register/"// MUST have trailing slash and be a unique dir.
 

# Gather the file name and add quotes for filemtime function.
$filename explode("$directory",$_SERVER['PHP_SELF']);

# The pages title
$title explode(".php",$filename[1]);
?>
<html>
<head>
<title><?php print ucfirst($title[0]); ?> | Members Area</title>
</head>
<body>
<?php
# Gather the ending time
$endTime explode(" ",microtime());
print 
"This page was last modified on " date ("F d Y"filemtime($filename[1])) . " and took " round($endTime[0] - $startTime[0],5)*100 " seconds to load.";
?>
</body>
</html>
Ya..
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Decaf is offline
View Public Profile Visit Decaf's homepage!
 
Old 11-18-2009, 12:16 PM Re: Dynamic title using PHP
kcmartz's Avatar
Super Talker

Latest Blog Post:
~Gri to Kcmartz~
Posts: 120
Name: Kenson
Location: Washington, USA
Trades: 0
Quote:
Originally Posted by Decaf View Post
What about a mix of both?

title.php
PHP Code:
$siteName 'Example';

$fileName $_SERVER['PHP_SELF'];
$fileName explode('.'$fileName);
$pageName $fileName[0];
$pageName str_replace('/'''$pageName);

print 
ucfirst($pageName) . " | $siteName"
Produces "{PAGE} | Example" aka.

Home | Google

Youwill only have to edit the $siteName variable, otherwise its set. All you need to do is include the script on your pages.

HTML Code:
<html>
<head>
<title><?php include("title.php"); ?></title>
...
</head>
<body>
...
</body>
</html>
It can't get any easier.
Just one thing: I see: "Testindex | Kcmartz.com" and "Testindex2 | Kcmartz.com" for the title! HELP! Otherwise, it works!!
__________________
Thanks,

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
for problems/site.
kcmartz is offline
View Public Profile Visit kcmartz's homepage!
 
Closed Thread     « Reply to Dynamic title using PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53426 seconds with 12 queries