Forum Moderators: coopster

Message Too Old, No Replies

Dynamically change META tags

         

expert_21

10:08 am on Dec 16, 2003 (gmt 0)

10+ Year Member



heh, i think the subject says it all. I am just wondering, if it is possible to dynamically change META tags through the use of PHP?

Any help is much appreciated. And yes, i am a beginner, on page 93 of Sam's PHP & MYSQL web development :) Thanks to all.

expert_21

10:09 am on Dec 16, 2003 (gmt 0)

10+ Year Member



ops forgot, I am looking to change the Title tag, and maybe the description one as well. Thank you.

Crispy_Beef

2:34 pm on Dec 16, 2003 (gmt 0)

10+ Year Member



You can change any part of a webpage with the use of PHP (or any other scripting language for that matter), and the title and meta tags aren't any different.

There's a few of ways you can do this, use a switch statement to use a set of tags for a specific page, have a database table containing the data, or you could just use a flat text file with the page names relating to the keyword etc.

I would stick with one of those methods, although you could code a function to scan each page as it loads and pull out interesting words from the content, leaving out non-specific words.

--
Crispy

caspita

2:43 pm on Dec 16, 2003 (gmt 0)

10+ Year Member



That's rigth, PHP is the one creating the web page before being sent to the browser so you have the whole control of aht you want tho change.

ie:

I suposso you have somethig like (I'll use * instead of '<' '>' html delimiters to avoid messing the forum, just in case)

*title*Current title here*/title*

then you have to use something like (usign *% for php delimiters)

*% // php code starts here
if ($condition == 1) echo ("*title*Title one here*/title*");
elseif ($condition == 2) ("*title*Title two here*/title*");
.
.
// ans so on

%* // php code terminates here

and you can do it with every html tag you want.

Hope this help.

Acternaweb

3:41 pm on Dec 16, 2003 (gmt 0)

10+ Year Member



Probably a dumb question, but why would you want to change the meta tags dynamically? Wouldn't that cause problems indexing?

Crispy_Beef

3:52 pm on Dec 16, 2003 (gmt 0)

10+ Year Member



You wouldn't be changing a specific pages meta tags each time the page is visited, but be providing a mechanism where a small chunk of code can serve the correct meta tags for hundreds of pages without having to optimise each individually.

For example I run a shop and base the meta tags on product pages, specifically that product and it's description, so each product page has a different title, description and set of keywords that relate to it, therefore each should be indexed as individual pages.

--
Crispy

expert_21

4:09 pm on Dec 16, 2003 (gmt 0)

10+ Year Member



that's exactly what i want. Thank you guys!

expert_21

4:12 pm on Dec 16, 2003 (gmt 0)

10+ Year Member



the reason i want to do that is simple..

under body, i have:

switch($_GET['category']){
case 'category1':
//content for category 1
break;

case 'category2':
//content for category 2
break;
}

each category has different content, so i want to change the title tag to show the category's title, not the default one. Hope this make sense :)

Crispy_Beef

4:22 pm on Dec 16, 2003 (gmt 0)

10+ Year Member



Yeah, that's the way. I have a function that I use:

function genMeta ($section) {
switch ($section) {

case 'catalogue':
// Code for catalogue page
break;

case 'about':
// Code for about page
break;

default:
break;
}
}

Then in my html I have the following:

<?php

// Code to grab the section var from the url here...

?>
<html>
<head>
<?php genMeta($section);?>
</head>

<body>
...whatever...
</body>
</html>

You can see how clean it makes your html too, so there's not lots of if else ifs about the place.

--
Crispy

lorax

3:35 am on Dec 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Probably a dumb question, but why would you want to change the meta tags dynamically? Wouldn't that cause problems indexing?

Not a dumb question at all. There are very good reasons for doing this. But the answers are only tips of the iceberg.

coopster

12:57 pm on Dec 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>>only tips of the iceberg.

Aw, c'mon now lorax, you have to give us a couple now...you can't just throw a statement like that out there without at least one or two details ;)

lorax

5:37 pm on Dec 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>> at least one or two details

LOL. Think time savings in editing and adding hundreds/thousands of pages of content displayed in only a handful of template pages. Think in terms of abbreviated content for intros and listings - from the same db records. There - I've said too much already.