homepage Welcome to WebmasterWorld Guest from 54.226.230.76
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
how to create dynamic titles with PHP
paff3




msg:1270872
 7:43 pm on Jan 10, 2003 (gmt 0)

Hi.Can i somehow make something like that:the title text is got somwhere inside body text.

 

korkus2000




msg:1270873
 7:43 pm on Jan 10, 2003 (gmt 0)

I don't understand.

paff3




msg:1270874
 7:51 pm on Jan 10, 2003 (gmt 0)

The script that makes me different title for each page and got the title text from somwhere inside <body></body>

korkus2000




msg:1270875
 7:54 pm on Jan 10, 2003 (gmt 0)

What language are you using? ASP, PHP, JSP?

paff3




msg:1270876
 7:55 pm on Jan 10, 2003 (gmt 0)

PHP is the language available for me.

rogerd




msg:1270877
 8:01 pm on Jan 10, 2003 (gmt 0)

paff, I'm no PHP expert but my guess is you'll have to restructure the code to put the head in the dynamic section, i.e., instead of generating the head and the top of the page, then doing all the dynamic stuff, you'll have to get the dynamic info first and then write the whole page.

paff3




msg:1270878
 9:15 pm on Jan 10, 2003 (gmt 0)

I didnt understand.Anyway would it be too big loss if i put same title for every page?

Finder




msg:1270879
 10:03 pm on Jan 10, 2003 (gmt 0)

I create the title dynamically by looking at the variables in the URL. It's not a perfect solution, but I think having an appropriate title is important.

For example: index.html?section=widgets&sub=blue

If there is no "sub" variable, the title is "My Site - Widgets". If the sub variable is there, I check it against a list of possible sub-titles: ["Blue Widgets", "Red Widgets"] and just append it: "My Site - Widgets - Blue Widgets"

It's not pretty, but I'm much happier having page titles, especially since I have heard it can improve search engine rankings.

jatar_k




msg:1270880
 10:18 pm on Jan 10, 2003 (gmt 0)

I think that creating the title on the fly from the content on the page would be a little too intensive. You could do it site wide when changes are made and store them in a db. You could write them by hand and store them in a db as well.

Finder's idea is a very nice one, hadn't thought of it. I, personally, would write them all by hand and then store them in a db and then call the proper one for the proper page using an include.

lorax




msg:1270881
 2:41 am on Jan 11, 2003 (gmt 0)

I'm having a hard time imagining what would prevent you from hard coding the titles to begin with - unless you are using a db. If using a db, then follow jatar_k's suggestion and just include the titles in the db (their own field). If not using a db then you've already built the pages yes? So why not add titles at that time? Of have I missed something extremely obvious? :)

keeper




msg:1270882
 2:55 am on Jan 11, 2003 (gmt 0)

I generally have a keyword-rich headline stored in the database, when displaying the content of my page, I request all the content, and for SEO purposes, store the headline text as a php variable and use that variable in the title and <h1> tags on the page.

Seems to work well in the search engines most of the time :)

brotherhood of LAN




msg:1270883
 3:28 am on Jan 11, 2003 (gmt 0)

I've been re-writing pages to grab info out a db, all the page info is made available by a variable in the filename, i.e. a number.

The number is the primary id for the page, providing title,meta description, page title etc.

IMHO its "lazy" in the fact the titles can be hard coded in certain pages, but for a situation like a "web directory" the dynamic creation of titles if pretty cool, i.e. for > breadcrumb > navigation

paff3




msg:1270884
 9:23 am on Jan 11, 2003 (gmt 0)

ok,my problem is like that:
I have allready made all my pages.
They are built up like that:

<?php
include "1.htm";
?>
<?php
include "2.htm";
?>
<?php
include "3.htm";
?>

title tag is inside 1.htm.
So if i change title,it changes in every page.
Im really stuck.

c3oc3o




msg:1270885
 10:11 am on Jan 11, 2003 (gmt 0)

In 1.htm, where the title tag is, write: <title><?=$title?></title>

And just define that variable before including it:
<?php
$title='My page';
include('1.htm');
?>

paff3




msg:1270886
 1:08 pm on Jan 11, 2003 (gmt 0)

I guess i have to make it your way allthough it takes forever.

aspdaddy




msg:1270887
 1:41 pm on Jan 11, 2003 (gmt 0)

>variable in the title and <h1> tags on the page

I used this a lot, also append it to the end of the meta description.

It is good if you notice a trend in the log files you can update the pages content very quickly i.e xmas/christmas :)

Do you think having H1 & title that match exactly is asking for trouble?

paff3




msg:1270888
 1:47 pm on Jan 11, 2003 (gmt 0)

I know one thing that could solve my problem.Lets say i have title inside body tag.Is there some script that moves title automatically inside head tag.It would really help.Hope you understand what i was trying to say.

andreasfriedrich




msg:1270889
 4:04 pm on Jan 11, 2003 (gmt 0)

Within the body tag you may only use the title attribute. The title element is not allowed.

If the title is hard coded within the body element (though I know of no sensible reason why it should be there) then you could postprocess your output by using output buffering and registering a call back function when starting output buffering [php.net]. A better solution would be to change all your documents. That way the server would not have to do the same work over and over again for each request.

If the title is not hard coded within the body element but the code to get it is currently within your include file that produces the body tag then you should move the code so that it gets executed right at the start of your script as rogerd suggested. c3oc3oīs suggestion is one way to get the title to appear in your first include after the code changes.

If you are willing to share what you do not understand about this approach then I am most certain that somebody here will gladly help you. Just saying I didnt understand sounds more like "I donīt like the advise and canīt be bothered to think about its merits."

Andreas

keeper




msg:1270890
 12:00 am on Jan 12, 2003 (gmt 0)

>>Do you think having H1 & title that match exactly is asking for trouble?

I've never had any trouble before (touching wood now), however, I do concatenate the company name to the title, which makes it a little harder to identify automatically.

For example:
<title>$headline - $companyname</title>
<h1>$headline</h1>

For some of my sites the abstract of the page is also in the database, which makes creating the description tag very easy.

<title>$headline - $companyname</title>
<meta name="description" content="$abstract">

<h1>$headline</h1>
<h2>$abstract</h2>

Leaves with me with training the data entry guys to create keyword rich headlines and abstracts....but thats not as easy as it sounds ;)

<added>paff3, is there anything in your database you can use as a title? otherwise it seems you are left with hardcoding them, or trying to harvest that information from your pages with a view to insert them into the database </added>

aspdaddy




msg:1270891
 9:30 am on Jan 13, 2003 (gmt 0)

Meta keywords dont have much weight but just for good measure :)
<meta keywords=".............." & split(headline,",") & ">"

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved