dynamic php title depends on page id and using of "if (isset"

- 1 answer

Ad

I'm trying to echo out dynamic php titles depends on page id for seo purposes.

First of all, all of my pages includes header.php

And this is how my header.php begins like:

include("database.php");

//connect tables in order to echo out titles

//1- connect to categories table if isset get category_id
if (isset($_GET["category_id"])) {
$query = $handler->query("SELECT * FROM categories WHERE category_id = ".$_GET['category_id']." ");
while($r = $query->fetch()) {
  $title = $r["title"];

  }
}

//2- connect to articles table if isset get article_id
if (isset($_GET["article_id"])) {
$query = $handler->query("SELECT * FROM articles WHERE article_id = ".$_GET['article_id']." ");
while($r = $query->fetch()) {
  $title = $r["title"];

  }
}

//3- connect to users table if isset get user_id
if (isset($_GET["user_id"])) {
$query = $handler->query("SELECT * FROM users WHERE user_id = ".$_GET['user_id']." ");
while($r = $query->fetch()) {
  $title = $r["username"];

  }
}

And after I feel free to echo out title like below:

 <title><?php if (isset($_GET["category_id"])) { echo $title; echo " |"; } 
              if (isset($_GET["article_id"])) { echo $title; echo " |"; } 
              if (isset($_GET["user_id"])) { echo $title; echo " |"; } ?> mypage.com</title>

*

This is result:

on category.php?category_id=1 Page title is: "Category 1 | mypage.com"

on article.php?article_id=1 Page title is: "Article 1 | mypage.com"

on users.php?user_id=1 Page title is: "Username 1 | mypage.com"

*

My question is, I am wondering if I over load the database with this structure?

When I'm on users.php?user_id=1 is php still execute this code?:

//1- connect to categories table if isset get category_id
if (isset($_GET["category_id"])) {
$query = $handler->query("SELECT * FROM categories WHERE category_id = ".$_GET['category_id']." ");
while($r = $query->fetch()) {
  $title = $r["title"];

  }
}

or because of I'm using if (isset($_GET["category_id"])) { on the begining I'm not connecting to database and there is no over load to database?

Ad

Answer

Ad

It seems okay but to get rid of too many conditions, I'd recommend another way. Generate the title on each page, assign it to a variable (before including your header.php) and then simply echo out the title.

Here is an example:

category.php:

<?php
    require_once"database.php";
    if(isset($_GET["category_id"])) {
        $query = $handler->query("SELECT * FROM categories WHERE category_id = ".$_GET['category_id']." ");
        while($r = $query->fetch()) {
        $title = $r["title"];
        }
    } else {
        // redirect somewhere else
    }
    include"header.php";
?>
    <body>
        Content goes here
    </body>
</html>

header.php:

<!DOCTYPE html>
<html>
    <head>
        <title><?php echo $title;?></title>
    </head>

Do the same for other files too. i.e. your users.php file will look like this:

<?php
    require_once"database.php";
    if (isset($_GET["user_id"])) {
        $query = $handler->query("SELECT * FROM users WHERE user_id = ".$_GET['user_id']." ");
        while($r = $query->fetch()) {
            $title = $r["username"];
        }
    } else {
        // redirect somewhere else
    }
    include"header.php";
?>
    <body>
        Content goes here
    </body>
</html>

header.php file will remain the same. This will save some of your server resources for sure as the server doesn't need to look for multiple criteria and the code becomes cleaner too.

Ad
source: stackoverflow.com
Ad