后端动态标题和描述?

时间:2013-03-29 17:33:41

标签: php dynamic backend

我有以下动态标题和说明的php代码:

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

但是,我堆叠使动态代码的后端(管理页面)列出并编辑标题和desc网站的每一页,如何制作?如果它需要每页不同的标题和不同的描述。请帮助逻辑和小代码。

谢谢

2 个答案:

答案 0 :(得分:1)

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

/www
    /core
        +/views
            page1.php
            page2.php
            page3.php

然后创建一个管理页面,我可以在其中修改这些文件的标题和标题。显然,我会通过在表中列出所有这些文件来创建表单:

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

<form>
    <?php 
        foreach (list_dir($path_to_views) as $file) { 
            if (is_readable($file))
                echo $file . ':' . '<input name="title"><input name="desc'>"
        }
    ?>
</form>

在后端,创建一个MySQL表,您可以在其中存储页面名称,标题和说明。我不打算在这里写完整个代码。如你所说,我只想提出一个想法。您可以从已插入的数据中填充表单,以便您可以编辑它们,然后添加stillig页面(尚未插入到数据库中)。

使用此方法,您不必在每次向网站添加新页面时手动创建(或编辑)表单。

干杯。

修改

你可以这样做:

例如about.php:

<?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'];
?>

并在管理页面中,存储上面已提到的页面名称,标题和说明。

编辑2

检查here以了解什么是PDO。无论如何,总之;我假设你创建了一个如下表:

-----------------------------------
| id | name | title | description |
-----------------------------------

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

假设你有一个这样的表结构,上面我用我显示的魔术常数确定文件名,并从数据库中取出文件名等于name值的行。

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

答案 1 :(得分:0)

首先,为了让它变得动态,你需要一种方法来存储这些值:$ title,$ desc somewhere。使用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);
?>

这是将值写入数据库的方法:

//表格接收者     

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

mysql_select_db("database" ,$link);

$sql = "INSERT INTO database.database (title, descripton) VALUES (" . $_POST['$title'] . "," . $_POST['$description'] . ")";

mysql_query($sql);

?>

//后端     

$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>

您将需要一个webform来收集值,以及将它写入数据库的PHP代码,虽然通常使用AJAX实际执行此操作,我将提供一个示例,它将重定向到php页面以提交值,更容易理解的例子:)

你最终可能会得到3个文件。 1个后端,1个前端,1个将表单处理到数据库中。为了创建我在我的示例中使用的数据库,您需要运行数据库查询:

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`) );