Reputation: 875
I'm trying to echo out dynamic php titles depends on page for seo purposes.
I successfully did this the pages I call from database depends on their id's.
Like that:
if (isset($_GET["category_id"])) {
$query = $handler->query("SELECT * FROM categories WHERE category_id = ".$_GET['category_id']." ");
while($r = $query->fetch()) {
$title = $r["title"];
}
}
And this is how I echo out:
<title><?php if (isset($_GET["category_id"])) { echo $title; echo " |"; } ?> mypage.com</title>
result:
on category.php?category_id=1
Page title is: "Category 1 | mypage.com"
*
But there are pages which is not static.
for example: index.php
, login.php
.
*
I want to figure out how to edit my code below to print "Login"
on login.php
between title tags.
<title>
<?php
if (isset($_GET["category_id"])) {
echo $title; echo " |";
}
?> mypage.com
</title>
EDIT
my login.php
is like that:
include("header.php");
content.
So I need to define $title
for login.php
in header.php
I need to add some codes to header.php
when user will see different title on login.php
, index.php
etc.
I'm able to do it category.php?category?id=1
already with the code above, but I need to also make it for login.php
, index.php
and so on.
Upvotes: 3
Views: 50
Reputation: 523
There are several ways to do this within the code you outlined.
First, I'm going to simplify some of your code a bit. This also potentially makes it slightly faster:
echo "<title>$title | mypage.com</title>";
This assumes that $title is going to be set, either by the query from when $_GET['category_id'] is set, or from the file that calls it. The great thing about includes is that they can pass variables. So in the login.php and any other file where you are not doing a GET, just specify $title in that file.
Login.php:
$title = 'Login';
include("header.php");
content.
Which would display page title of "Login | mypage.com".
Upvotes: 2