Jump to content


Photo

I want to use php to generate title tags


  • Please log in to reply
19 replies to this topic

#1 animedls

animedls
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 27 September 2006 - 03:45 PM

Hi all , im new to this forum and have posted this question elsewhere on other forums but have not been able to get an answer .

i have purchased a site www.animedls.com about 1 month ago and i am in the middle of updating the site and doing some seo work on it. I believe that my site runs from a template and it just inserts the .php page into the template (i dont know what template it uses). If you have a look at site , the title tags stay the same for all the pages .

My question is how do i generate title tags for each individual page so that the title tag relates to the actual .php and i do not just have one title tag for all pages in the site ?

I am noob when it comes to php so any help is very much appreciated

#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 27 September 2006 - 04:01 PM

The principal could be like this example, include title-php stuff above the html title tag:


<?php

switch($_GET['page'])
{
  case 'home':
  $title = "Welcome";
  break;
  
  case 'about':
  $title = "About us";
  break;
  
  // etc etc
}

?>

<!-- html starts -->

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


So for example "index.php?page=about" would generate the title "About us" in this case.

If you query for example mysql for articles and want the article title as html title, run query instead of the switch example and declare article title as $title.

#3 animedls

animedls
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 28 September 2006 - 08:33 AM

hi , can you explain a little simpler , ima noob to php. could you do it in steps for me ?

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 September 2006 - 09:38 AM

Hi welcome to phpfreaks.com

I would recommend you to read up on the switch statement if you dont understand the code. it is basically an if/elseif statement.

But this is what the code is doing.

It is grabing a url variable called page (example url: mysite.com/index.php?page=home) you can access this variable by using $_GET['page']. Now it creates a variable called title ($title) based on the value of the page variable ($_GET['page']). So if page is set to home, it'll trigger the first case in the switch, which is home and set the title variable to hold the string Welcome. If its not home it'll check to see if page is set to about. If its it'll set the title variable to hold the string About Us.

Now on this line it echos out the titile variable:
<title><?php echo $title; ?></title>

Hope that helps. The manual shoudl be able to clear up on how the swtich statement actually works.

#5 animedls

animedls
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 28 September 2006 - 09:57 AM

by doing this, is this something that the search engines could spider ?

#6 mendoz

mendoz
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts

Posted 28 September 2006 - 10:13 AM

Yeah, because the output is for example:

for the page index.php?page=home
the output will be
<title>home</title>

#7 steveclondon

steveclondon
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 28 September 2006 - 10:18 AM

This is also a good thing because search engines will also look at the name of you page and if it sees keywords in this as well that can help. If you wanted to go one better you could do a mod-rewrite. Anyway sorry I have strayed a bit from what you were asking there.

Have you looked at keywords and description, you should still have unique keywords and descriptions on your pages, although some say not.

#8 animedls

animedls
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 28 September 2006 - 10:21 AM

so i could generate title tags by php as well as the meta desciption and meta keywords tags?

#9 steveclondon

steveclondon
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 28 September 2006 - 10:26 AM

yes you could. How easy that is depends on the data already available to you or how you structure your data. If you are willing to through some time in and plan it out first then you could do a fairly effective range unique keywords and descriptions.


#10 animedls

animedls
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 28 September 2006 - 10:32 AM

woudl anyone be willing to do the title tags code on one page for me so i can see the structure of the code and then i could replicate this on all other pages/for other pages . I will give you cpanel access , just one live example and then i'll be able to replicate for all the other pages i have

#11 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 September 2006 - 02:42 PM

By live example you mena something like this:
<?php
// check that page is set and that its not empty, if it is set and not empty we'll use the page variable
// else we'll set it to a efualt value which is home
$page = isset($_GET['page']) && !empty($_GET['page']) ? $_GET['page'] : 'home';

switch($page)
{
    case 'home':
        $title = "Welcome";
        $content = 'Welcome home dude!';
    break;

    case 'about':
        $title = "About us";
        $content = 'so you wanno know stuff about me!';
    break;

    case 'products':
        $title = 'My products';
        $content = 'Hey the following are my products';
    break;

    default:
        $title = '404 Error';
        $content = "Sorry the page '<i>" . $page . "</i>' you reguested was not found. Please try again";
    break;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title><?php echo $title; ?></title>
</head>
<body>

<h1><?php echo $title; ?></h1>
<p><?php echo $content; ?></p>

<p>
  <hr />
  Pages: <a href="?page=home">Home</a> | <a href="?page=about">About Us</a> |
  <a href="?page=products">Products</a> | <a href="?page=duffPage">A duff page</a>
</p>

</body>
</html>


#12 animedls

animedls
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 28 September 2006 - 02:47 PM

i think i unedrstand but i tkae it , i would have tochaneg all my urls/links on the site to the format "?page=blahblah"

I only include this code on the index page right ?



#13 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 28 September 2006 - 02:58 PM

If your e-commerce store is running off a database you may try something like

<? include("config.php"); ?>
<title><?
// Get 1 Category Title Below
$result = mysql_query("SELECT *
    FROM womensproducts
        WHERE CategoryID='$cat' limit 1") or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
include("title.php");
}
mysql_free_result($result);
?>

Which my title.php just includes:  <?=$row["Category"]; ?>

You can change this variable to whatever your wanting to change on each page.

Hope this helps.

#14 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 September 2006 - 03:01 PM

i think i unedrstand but i tkae it , i would have tochaneg all my urls/links on the site to the format "?page=blahblah"

I only include this code on the index page right ?

No. Just chnage page in the $_GET var to the name of the variable that holds the page that is being reguested.

What is the format of the urls?

#15 animedls

animedls
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 28 September 2006 - 03:08 PM

examples of my urls :

http://www.animedls.... anime episodes

http://www.animedls.... bebop episodes



#16 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 September 2006 - 03:10 PM

Umm looks like you're using mod_rewrite. Could you post the the urls in their raw state you should be able to get these by looking in the htaccess file that does the mod_rewrite.

#17 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 28 September 2006 - 03:50 PM

This will be a variant:
<?php

$is_url = $_SERVER['REQUEST_URI'];
$is_piece = explode(",", $is_url);
$title = urldecode($is_piece[1]);

?>

On your first link example the title then becomes "complete anime episodes"
On the second link it is "complete cowboy bebop episodes"

#18 animedls

animedls
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 28 September 2006 - 03:55 PM

im completely lost , ive hired a freelancer for $80 to complete work

#19 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 28 September 2006 - 04:08 PM

All you'de have to do was include my last code on top of every page and echo $title out within the title tags.
But if you give up and decide to hire a freelancer on this issue, you obviously aren't seeking to learn the magic of php  8)

#20 animedls

animedls
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 28 September 2006 - 04:12 PM

was trying to but got to hard to understand and ddidnt want to mess up site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users