-1

我需要帮助。在我的网站上,我使用 URL 参数来决定要在页面上包含哪些其他 PHP 文件。我的第一个问题是:index.php 中应该有什么,包含的 PHP 文件中应该有什么?

在互联网上我找到了说明,为 index.php 建议了这个结构:

<html>
    <head>
        <?php include 'header.php'; ?>
    </head>
    <body>
        <?php include 'menu.php'; ?>
        <?php include 'content.php'; /* Including page based on the parameters in the url */ ?>
    </body>
</html>

使用这种结构,如何<head>根据 中的内容更改部分中的数据content.php?例如,对于index.php?option=article&id_article=1,我将包含article.php并显示 id 为 1 的文章。那么,如何更改在包含文章之前编写的<title><meta> 等内容?<head>

谢谢!

4

3 回答 3

1

一种丑陋但可行的选项是让 header.phpecho简单地设置变量,例如$titleand $meta[]。也不是让 article.php 从回显中返回一个变量,如$html. 同样在 article.php 中,您可以覆盖在 header.php 中设置的任何变量。然后你可以像这样构造你的 index.php:

<?php include 'header.php'; ?>
<?php include 'article.php'; ?>
<html>
<head>
    <?php echo $title ?>
</ head>
<body>
<?php include 'menu.php'; ?>
<?php echo $html ?>
</ body>
</ html>

或者你可以看看ob_start()等等ob_flush()...

于 2013-07-03T15:20:44.453 回答
0

为了尽可能简单,您可以将标题设为函数,然后在其他地方调用该函数......

示例(未经测试):

function drawHeader($title, $desc = "", $keywords = "", $extra = "") {
  ?>
  <head>
    <title><?php echo $title; ?></title>
    <meta name="description" content="<?php echo $desc; ?>">
    <meta name="keywords" content="<?php echo $keywords; ?>">
    <link rel="stylesheet" type="text/css" href="path/to/my.css">
    <?php echo $extra; ?>
  </head>
  <?php
}

上述目标是让您可以轻松快速地执行类似...

<!DOCTYPE html>
<html>
<?php drawHeader("Home", "Some description", "Some, keywords, here"); ?>
<body>
  <h1>Hello, world</h1>
  <?php drawFooter(); // maybe a footer of some type? ?>
</body>
</html>

或者您可以在调用包含的文件之前设置变量......并且在包含的文件中只需在适当的位置回显这些值。

有很多方法可以做到这一点,有很多标准和最佳实践、框架、模板系统、使用输出缓冲的占位符等。

于 2013-07-03T15:19:55.840 回答
0

首先,您找到的说明没有什么可学习的

作为第二个获取文件 article.php 的内容

使用网址index.php?option=article&id_article=1

您将需要使用$_GET['id_article']

例子 :

$page = $_GET {'id_article'};
if (isset($page)) {
$url = $page.".php";
    if (file_exists($url)) {
        include $url;
    }

您可以使用数据库来存储文章并使用查询然后使用

if ($_REQUEST['id_article'] == $page) {
    $querythearticle = mysql_query("select tablename from database_name where id_article='$page'");

}
于 2013-07-03T15:30:28.147 回答