简体   繁体   English

动态php标题取决于页面ID和使用“if(isset”)

[英]dynamic php title depends on page id and using of “if (isset”

I'm trying to echo out dynamic php titles depends on page id for seo purposes. 我试图回应动态PHP标题取决于页面ID为seo目的。

First of all, all of my pages includes header.php 首先,我的所有页面都包含header.php

And this is how my header.php begins like: 这就是我的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 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" category.php?category_id=1页面标题是:“Category 1 | mypage.com”

on article.php?article_id=1 Page title is: "Article 1 | mypage.com" article.php?article_id=1页面标题是:“Article 1 | mypage.com”

on users.php?user_id=1 Page title is: "Username 1 | mypage.com" users.php?user_id=1页面标题是:“用户名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?: 当我在users.php?user_id=1是php还是执行这段代码吗?:

//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? 或者因为我正在使用if (isset($_GET["category_id"])) {在开始我没有连接到数据库并且数据库没有过载?

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. 在每个页面上生成标题,将其分配给变量(在包含header.php之前),然后简单地回显标题。

Here is an example: 这是一个例子:

category.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: header.php文件:

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

Do the same for other files too. 对其他文件也一样。 ie your users.php file will look like this: 即您的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 file will remain the same. header.php文件将保持不变。 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. 这将节省一些服务器资源,因为服务器不需要查找多个条件,代码也变得更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM