The dynamic name of php depends on the id of the page and the use of "if (isset")

I am trying to track dynamic php names depending on the page id for seo purposes.

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

And this is how mine begins header.php:

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 repeat the title as shown 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 the result:

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

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

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

*

My question is: I wonder if I can load a database with this structure?

When I'm on users.php?user_id=1, is php still executing 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"];

  }
}

- , if (isset($_GET["category_id"])) { , ?

+4
1

, , . , ( header.php), .

:

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>

. users.php :

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

+4
source

Source: https://habr.com/ru/post/1620976/


All Articles