Jump to content

Archived

This topic is now archived and is closed to further replies.

animedls

I want to use php to generate title tags

Recommended Posts

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

Share this post


Link to post
Share on other sites
The principal could be like this example, include title-php stuff above the html title tag:

[code]

<?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>

[/code]

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.

Share this post


Link to post
Share on other sites
Hi welcome to phpfreaks.com

I would recommend you to read up on the [url=http://php.net/switch]switch statement[/url] 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 [b]url variable[/b] called [b]page[/b] (example url: mysite.com/index.php?page=home) you can access this variable by using [b]$_GET['page'][/b]. Now it creates a variable called [b]title[/b] ($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 [b]Welcome[/b]. 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 [b]About Us[/b].

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

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

Share this post


Link to post
Share on other sites
Yeah, because the output is for example:

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
By live example you mena something like this:
[code]<?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>[/code]

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote author=animedls link=topic=109704.msg443099#msg443099 date=1159454831]
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 ?
[/quote]
No. Just chnage [b]page[/b] 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?

Share this post


Link to post
Share on other sites
examples of my urls :

http://www.animedls.com/content-media,complete%20anime%20episodes

http://www.animedls.com/content-media,complete%20cowboy%20bebop%20episodes

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
This will be a variant:
[code]
<?php

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

?>
[/code]

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

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.