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

Reply
how to add <title> tag in php?
Old 03-09-2009, 04:14 AM how to add <title> tag in php?
Skilled Talker

Posts: 55
Name: Adrian
Trades: 0
Hi,

I have just about completed my first php website and am not sure how to add a title tag for each of the pages? I have used <title> Title </title> tag but the title is not coming up at the top of any of my pages.

Is there another way title tags should be written when developing a site in php as to opposed from the traditional XHTML sites?

regards, elv8
elv8 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-09-2009, 04:37 AM Re: how to add <title> tag in php?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by elv8 View Post
Hi,
Is there another way title tags should be written when developing a site in php as to opposed from the traditional XHTML sites?
I think you are misunderstanding the relationship between php and (X)HTML. PHP is server side code, and is parsed by the server. HTML is client side code and is parsed by a user's browser. PHP code never gets sent to a user's browser so it has no impact on the actual markup that the user parses. In other words, there is no alternate or special way to format your output when using PHP because the browser is not even aware that the page is generated by PHP.

Using title tags should be identical to how you would in plain xhtml, but you can use php to set the title dynamically:

PHP Code:
<?php
$title 
'myTitle';

echo 
'<title>' $title '</title>';
?>
OR

PHP Code:
<?php
$title 
'myTitle';
?>

<title><?php echo $title?></title>
I'm not sure exactly why your current version is not working. Post your source code and I (or someone else) will probably be able to figure it out.
__________________

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
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-09-2009, 05:32 AM Re: how to add <title> tag in php?
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
Maybe it's because you are putting the <title> tag outside of the header now.
What I always do to fix that, is first include all the PHP and stuff.
Then make everything be stored in variables.
Then inside the HTML, echo those strings on the right places.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 03-09-2009, 08:34 AM Re: how to add <title> tag in php?
Skilled Talker

Posts: 55
Name: Adrian
Trades: 0
Thanks for your comments and have fixed the problem now

Sweet
elv8 is offline
Reply With Quote
View Public Profile
 
Old 03-10-2009, 09:59 AM Re: how to add <title> tag in php?
Skilled Talker

Posts: 52
Name: Morten
Trades: 0
Nullpointer...

Why would you make so much code for such a little thing?

<?php
echo "<title>TITLE</title>";
?>

1 line of code? instead of "that"... That's just a waste of size
__________________
- Do Nothing!
- Get Nothing!

Please login or register to view this content. Registration is FREE
Rotco88 is offline
Reply With Quote
View Public Profile
 
Old 03-10-2009, 02:16 PM Re: how to add <title> tag in php?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by Rotco88 View Post
Nullpointer...

Why would you make so much code for such a little thing?

<?php
echo "<title>TITLE</title>";
?>

1 line of code? instead of "that"... That's just a waste of size
The example is trivial, as many code examples are. The point was to show him that you can set the title dynamically rather than just outputing a static title.
__________________

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
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-10-2009, 07:21 PM Re: how to add <title> tag in php?
Defies a Status

Posts: 1,606
Trades: 0
Quote:
Why would you make so much code for such a little thing?
Because that is the way you would do if you were pulling it from a database, a file or parsing a portion of some text to create it.
__________________
Colbyt

Please login or register to view this content. Registration is FREE
colbyt is offline
Reply With Quote
View Public Profile
 
Old 03-10-2009, 07:36 PM Re: how to add <title> tag in php?
Decaf's Avatar
Ultra Talker

Posts: 489
Name: Adam
Trades: 0
Quote:
Originally Posted by Rotco88 View Post
Nullpointer...

Why would you make so much code for such a little thing?

<?php
echo "<title>TITLE</title>";
?>

1 line of code? instead of "that"... That's just a waste of size
Not to burst your bubble but that is 3 lines of code. :P

The example above was just a preview. You can also set the title based on the webpage (Ex. 'about.php', 'contact.php').

Ive posted about that somewhere before.
__________________

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
Reply With Quote
View Public Profile Visit Decaf's homepage!
 
Old 03-11-2009, 09:09 AM Re: how to add <title> tag in php?
Skilled Talker

Posts: 55
Name: Adrian
Trades: 0
thanks for that short line of code, it was very helpful.
elv8 is offline
Reply With Quote
View Public Profile
 
Old 03-19-2009, 11:25 AM Re: how to add <title> tag in php?
Junior Talker

Posts: 1
Trades: 0
Hi folks,

Here's an alternative for you...

I use templates on my website and I've just realised that I'm not using any meta tags or title tags properly.

There are essentially three parts to my site.
1) The core of the site
2) Reviews section (y2neil.com/reviews)
3) Blog (y2neil.com/blog)

The blog is in Wordpress thus covered for meta/title but the reviews is not.

I want to be able to use PHP to change the title and meta tags in the header dependant on where the user is. Example if in the core section it should just be
<title>Y2Neil.com</title>
If in the review section something like
<title>Y2Neil.com | Reviews | Review Title</title>

Any tips on how I can do this considering all parts of the website use the same template.

Many thanks,

Last edited by Y2Neil; 03-19-2009 at 11:28 AM.. Reason: typo
Y2Neil is offline
Reply With Quote
View Public Profile
 
Old 03-19-2009, 11:54 AM Re: how to add <title> tag in php?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Something like this should do it:
PHP Code:
<?php
$title 
'Y2Neil.com';
if(
$_SERVER['PHP_SELF'] == '/reviews.php')
{
     
$title .= ' | Reviews | ' $reviewTitle;
}
?>
<title><?php echo $title ?></title>
__________________

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
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-20-2009, 05:00 AM Re: how to add <title> tag in php?
Junior Talker

Posts: 1
Name: Admaster
Trades: 0
If I had many different titles , how should I change Title for each page ? rather should I push ?pagetitle=Mytitle for these page ?

Any good options to chang ethe title each time if you are using a template page. and just including content part by php include .


Admaster
<removed create a signature>

Last edited by chrishirst; 03-20-2009 at 07:49 AM..
indiavision is offline
Reply With Quote
View Public Profile
 
Old 03-20-2009, 05:41 AM Re: how to add <title> tag in php?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Create a header file that you can include in each page.

Header.php
PHP Code:
<?php
$title

switch(
$_SERVER['PHP_SELF'])
{
case 
'/index.php':
     
$title 'Home';
     break;
case 
'/PageX.php':
     
$title 'Page X';
     break;
//  .
//  .
//  .
}

echo 
'<title>'.$title.'</title>';
?>
__________________

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
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-28-2009, 05:11 PM Re: how to add <title> tag in php?
Junior Talker

Posts: 4
Name: Georgia
Location: Hellas
Trades: 0
sorry for the double post...

Last edited by Georgia; 03-28-2009 at 05:18 PM..
Georgia is offline
Reply With Quote
View Public Profile Visit Georgia's homepage!
 
Old 03-28-2009, 05:16 PM Re: how to add <title> tag in php?
Junior Talker

Posts: 4
Name: Georgia
Location: Hellas
Trades: 0
Hi people.
Well i am new in php and my problem is that my dynamic tags do not work for me. I will be pleased for any assistance - solution.

Here is my "index.php"

PHP Code:
<?php include ("header.php"); ?>
</head>
<body class="twoColFixRtHdr">

<div id="container">
  <div id="logo">
    <?php include ("logo.php"); ?>
  </div>
  <div id="menu">
    <?php include ("menu.html"); ?>
  </div>
  <div id="sidebar1">
    <?php include ("news.php"); ?>
    </div>
  <div id="mainContent">
    <?php

if (isset($_GET['pg']) && $_GET['pg'] != "") {

$pg $_GET['pg'];

if (
file_exists(''.$pg.'.php')) {

@include (
''.$pg.'.php');

} elseif (!
file_exists('new/'.$pg.'.php')) {

echo 
'this page is unavailble';

}

} else {

@include (
'main.php');

}

?>
    </div>
    <br class="clearfloat" />
  <div id="footer">
  <?php include ("footer.php"); ?>
    </div>
 </div>
</body>
</html>
Here is my "header.php" witch goes like this:
PHP 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>
<title><?php if(isset($title)) { print $title; } else { print "Default title goes here"; } ?></title>
<meta name="keywords" content="<?php if(isset($keywords)) { print $keywords; } else { print "keyword1,keyword2,keyword3,"; } ?>" />
<meta name="description" content="<?php if(isset($description)) { print $description; } else { print "Default description tag goes here."; } ?>" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="css/panev.css" rel="stylesheet" type="text/css" media="all" />
And finaly here is one of my many included files, for instance the "main.php":
PHP Code:
<?php

$title 
"My Page.";
$keywords "Georgia, internet, homeland.";
$description "My personal page has fun.";
require_once(
"header.php");

?>
        
<h1>CHAPTER</h1>
<div align="justify">blah, blah, blah, blah, blah, blah. </div>

Thanks in advance.
Georgia is offline
Reply With Quote
View Public Profile Visit Georgia's homepage!
 
Old 03-28-2009, 05:32 PM Re: how to add <title> tag in php?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
What is the problem exactly? Are you getting an error message?

Also for future reference you may want to start a new thread rather than reply to an old one.
__________________

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
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-28-2009, 05:39 PM Re: how to add <title> tag in php?
Junior Talker

Posts: 4
Name: Georgia
Location: Hellas
Trades: 0
Thanks for the response.
Quote:
Originally Posted by NullPointer View Post
Are you getting an error message?
As i said i am new with php. I dont know where to check for errors.
Actually i am working on a local host using MAMP.

The problem is that the dynamic tags (title etc) on preview do not work for me

Last edited by Georgia; 03-28-2009 at 05:42 PM..
Georgia is offline
Reply With Quote
View Public Profile Visit Georgia's homepage!
 
Old 03-28-2009, 06:27 PM Re: how to add <title> tag in php?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
If title is set in main.php, and called in header.php, then main.php must be included before header.php otherwise it appears that title is not set.
__________________

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
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-28-2009, 06:49 PM Re: how to add <title> tag in php?
Junior Talker

Posts: 4
Name: Georgia
Location: Hellas
Trades: 0
Well, the tags are working, only for "main.php" and with a new problem: The info of the "header.php" appears twise when browsing my index.php (view source), once on the top (wich is correct), and a second, right on top of the article (main.php)!

Last edited by Georgia; 03-29-2009 at 10:54 AM..
Georgia is offline
Reply With Quote
View Public Profile Visit Georgia's homepage!
 
Reply     « Reply to how to add <title> tag in 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.52916 seconds with 12 queries