2

我有以下代码来动态包含页面:

<div id="content">
    <div id="aside">
        ...
    </div>

    <div id="main">
        <?php
        $page = (isset($_GET['page'])) ? sanitize($_GET['page']) : 'home';
        if (!include 'pages/'.$page.'.php') require 'pages/404.php';
        ?>
    </div>
</div>

如您所见,#aside具有静态内容。我想根据所选页面
包含特定内容。#aside例如,如果用户转到“主页”和“关于”,我希望将“默认”放在一边。但是,如果用户转到“文档”,我希望将“部分”放在一边。
我知道我可以将每个页面都包含在内,但这并不有效。我也不希望用户能够将放在一边作为主要内容,因此它们必须位于不同的文件夹或其他东西中。
我想知道一种有效且不那么复杂的方法来做到这一点。

感谢您花时间阅读本文。

4

4 回答 4

2

您希望在数据库中的哪个页面上保留哪个侧边栏,然后查询该数据库以包含正确的侧边栏。

表结构可能如下所示:

  • 表格侧边栏:ID | 路径 | 姓名 | 更多信息在侧边栏...
  • :ID | 路径 | 姓名 | 更多信息在页面...
  • 表格侧边栏到页面:page_ID | sidebar_ID

这种方法甚至允许您在特定页面上放置多个侧边栏。

于 2013-01-06T21:27:01.787 回答
1

有几种不同的方法可以做到这一点,它们或多或少都是平等的。我几乎总是为站点使用 config.php 文件来保存我希望每个页面拥有的任何全局信息。在每一页的顶部,您只需调用

<?php 

    require_once('config.php');

?>

在该 config.php 文件中,您可以有一个数组列出您的页面名称和您希望为每个页面包含的文件,以及一个返回内容的函数,如下所示:

// this lets you call includes relative to the site root
set_include_path($_SERVER['DOCUMENT_ROOT']);

$defaultAsideContent = 'includes/default.php';

$asideContent = array(
    'index.php' => 'includes/include-1.php',
    'document.php' => 'includes/include-2.php'
);

function getAsideContent() {

    global $asideContent;
    global $defaultAsideContent;

    $content = $defaultAsideContent;

    // get the requested page
    $pageFull = $_SERVER['REQUEST_URI'];
    // strip URL variables
    $pageParts = explode('?', $pageFull);
    $page = $pageParts[0];

    // loop throught the array and see if there is specific aside content for the page
    foreach($asideContent as $key=>$value) {
        if ($page == $key) {
           $content = $asideContent[$key]);
        }
    }

    include($content);
}

最后,无论你想让你的旁白内容出现在哪里,只要做

<?php getAsideContent(); ?>

当你创建一个新页面时,如果你想要特定的内容,只需编辑你的配置文件。仅供参考,根本没有测试这个,可能有错误,但你明白了。

于 2013-01-09T21:53:14.800 回答
1

如果你这样做了怎么办?

<?php
ob_start();
$page = (isset($_GET['page'])) ? sanitize($_GET['page']) : 'home';
if (!include 'pages/'.$page.'.php') require 'pages/404.php';
$contents = ob_get_clean();
?>
<div id="content">
    <div id="aside">
        <?php include($aside); ?>
    </div>

    <div id="main">
        <?php echo $contents; ?>        
    </div>
</div>

$page.php 看起来像:

<?php $aside = "sidebars/default.php"; ?>
<p>HTML for #main<br />
goes here</p>
于 2013-01-09T02:36:55.440 回答
0

Thank you all for your answers and collaboration. Although none of the answers did exactly what I was looking for, they showed me other ways to approach this issue and guided me to decide what method to use.
I came up with what I think is the simpliest way to do this:

  1. I set my folder structure as: pages/aside and pages/main
  2. I set up an array($asides) with the aside files as the keys and the main content files as the values.
  3. Then I check if the requested file exists in the main folder.
  4. If it doesn't exist, I redirect the user to the 404 page. If it does exist, I loop through $asides to see which aside is asigned to that main content page.
  5. If it doesn't belong to any of the establisged asides, then I include the default aside.
$asides = array(
        'aside1' => array('page1', 'page2', 'page3', 'page4'),
        'aside2' => array('page5', 'page6')
    );

$page = (!empty($_GET['p'])) ? sanitize($_GET['p']) : 'page1';

if (file_exists("pages/main/{$page}.php")) {

    foreach ($asides as $key => $value) {
        if (in_array($page, $asides[$key])) {
            $aside = $key;
            break;
        }
    }
    if (!isset($aside)) $aside = 'default';

?>
    <div id="aside"><?php require "pages/aside/{$aside}.php"; ?></div>
    <div id="main"><?php require "pages/main/{$page}.php"; ?></div>
<?php

} else {
    header('Location: ?p=404');
}


The bounty goes to Madara Uchiha because in my opinion, his answer is simple an effective. Thanks again to all of you who helped me with this issue.

于 2013-01-10T15:23:26.550 回答