Php 后端动态标题和描述?

Php 后端动态标题和描述?,php,dynamic,backend,Php,Dynamic,Backend,我有用于生成动态标题和描述的php代码,如下所示: <title><?php echo $title; ?></title> <meta name="description" content="<?php echo $desc; ?>"> 首先,为了使其动态化,您需要一种存储这些值的方法:$title、$desc。使用PHP时最常见的方法是将它们存储在数据库中 因此,前端(上面由您提供)将从数据库中检索值$title和$desc,而

我有用于生成动态标题和描述的php代码,如下所示:

<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $desc; ?>">


首先,为了使其动态化,您需要一种存储这些值的方法:$title、$desc。使用PHP时最常见的方法是将它们存储在数据库中

因此,前端(上面由您提供)将从数据库中检索值$title和$desc,而后端将允许您编辑这些值,从而更新保存在数据库中的记录

您还没有提供有关当前使用的任何存储的任何信息,因此我将随机选择MySQL作为数据库,并提供一个示例

以下是从数据库获取值的方式:

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$result = mysql_query('SELECT title,descripton FROM database.database WHERE id=1');
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

while ($row = mysql_fetch_assoc($result)) {
    $title =  $row["title"];
    $desc = $row["desc"];
}

mysql_close($link);
?>
//后端
$result = mysql_query('SELECT title,descripton FROM database.database WHERE id=1');
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

while ($row = mysql_fetch_assoc($result)) {
    $title =  $row["title"];
    $desc = $row["desc"];
}

mysql_close($link);
?>
<form action="php-form-processor.php" method="post">
<input type="text" name="title" maxlength="50" value="<?=$title;?>" />
<input type="text" name="description" maxlength="50" value="<?=$description;?>" />
<input type="submit" name="formSubmit" value="Submit" />
</form>

为了防止每次创建页面时都更新代码,我会创建一个文件夹来存储页面。例如:

/www
    /core
        +/views
            page1.php
            page2.php
            page3.php
然后创建一个管理页面,我可以在其中修改这些文件的标题和标题。显然,我将通过在表中列出所有这些文件来创建表单:

(您可以查看如何列出文件夹中的文件)

其中ID是主键,name是文件名,title是
标记内的标题,description是
meta
标记的
描述
属性

假设您有一个这样的表结构,在上面,我将使用我显示的神奇常量确定文件名,并从数据库中获取文件名等于名称值的行


如果您不理解代码的工作原理,请阅读我提供的参考资料

回答很宽泛。@Rikesh:我不这么认为,请回答逻辑和litle代码。thanksI只能说为每页的
title
desc
保存一行。我真的很感谢你友好的回答,但我仍然很困惑,机器如何知道,不同的页面[即主页,关于我们,联系我们,博客]?我想使用ID,但如何在后端或前端制作?请帮忙。thanks@LenaQueenSavas Vedova:谢谢你的友好回答,但我真的不明白你的短代码。请你写更多的代码或使用codepen或任何东西或你的博客。很抱歉,我非常感谢您的善意帮助。@LenaQueen您还不明白什么?关于“name=:fn”,请您详细介绍一下好吗?谢谢
CREATE SCHEMA `database` DEFAULT CHARACTER SET latin1 ;

CREATE  TABLE `calendar`.`database` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `title` VARCHAR(225) NOT NULL ,
  `description` TEXT NOT NULL ,
  PRIMARY KEY (`id`) );
/www
    /core
        +/views
            page1.php
            page2.php
            page3.php
<form>
    <?php 
        foreach (list_dir($path_to_views) as $file) { 
            if (is_readable($file))
                echo $file . ':' . '<input name="title"><input name="desc'>"
        }
    ?>
</form>
<?php 
    // Use PDO for database connections.
    // Manual: http://php.net/manual/en/book.pdo.php
    $con = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

    // Get name of the page.
    // The __FILE__ magic constant will give you the full path to the file that is calling the constant. Basename will strip the path and give you only the filename. At the end, split the string by excluding the .php part and get the file name.
    $pagename = current(explode('.', basename(__FILE__)));

    // Select your title and description where name equals to file name.
    $stmt = $con->prepare('SELECT title, description FROM myTable where name = :fn');
    $stmt->bindValue(':fn', $pagename, PDO::PARAM_STR);

    // Considering that you stored a unique name, the result should return a unique row so there is no need for fetchAll.  
    $result = $stmt->fetch();

    echo $result['title'];
    echo $result['description'];
?>
-----------------------------------
| id | name | title | description |
-----------------------------------