【问题标题】:How can I include an unique sidebar content for each page with php如何使用 php 为每个页面包含唯一的侧边栏内容
【发布时间】:2016-02-21 05:49:24
【问题描述】:

我正在为我的网站构建一个结构,但我的侧边栏有问题。我希望每个页面都有一个独特的侧边栏内容,但我不知道该怎么做。我创建了一个 CSS 文件夹以在我的 style.css 的根目录下放置一个名为 index.php 和 template.php 的文件,一个用于对我的所有页面进行分组的 pages 文件夹,并在此文件夹中创建一个名为 index.php 的文件。

根目录下的 index.php 有这个内容(它允许在每个不同的页面中定义每个内容):

<?php

if(!isset($_GET["p"]))
{
    $_GET["p"]="index";
}

if(!file_exists("pages/".$_GET["p"].".php"))
{
    $_GET["p"]="404";
}

ob_start();

include "pages/".$_GET["p"].".php";

$page_content = ob_get_contents();

ob_end_clean();

include "template.php";

?>

我的模板文件:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title><?= $page_title; ?></title>
        <link href="css/style.css" rel="stylesheet">
    </head>
    <body id="<?= $page_id; ?>">
        <div id="header">
            <div class="wrapper">
                <div id="logo">
                    <a href="#">Logo</a>
                    <p>Pellentesque in velit egestas, mattis metus vitae, hendrerit nunc. Nulla facilisi. Vestibulum at felis nec nunc ornare semper eu sit amet massa.</p>
                </div>
            </div>
        </div>
        <div class="wrapper">
            <div id="navigation">
                <ul class="unstyled-list">
                    <li>
                        <a href="#" class="home active">Accueil</a>
                    </li>
                    <li>
                        <a href="#">Utilisateurs</a>
                    </li>
                    <li>
                        <a href="#">Achats</a>
                    </li>
                    <li>
                        <a href="#">À propos de nous</a>
                    </li>
                </ul>
            </div>
            <div id="container">
            <?php if($sidebar): ?>
                <div id="main-sidebar-divider">
                    <div id="second-sidebar-divider">
            <?php endif; ?>
                <h1 class="page-title"><?= $page_title; ?></h1>
                <?= $page_content; ?>
            <?php if($sidebar): ?>
                    </div>
                </div>
                <div id="sidebar">
                    <?= $sidebar_content; ?>
                </div>
            <?php endif; ?>
            </div>
        </div>
    </body>
</html>

如您所见,我有一个变量 $page_content。这让我可以为每个页面拥有一个独特的内容,但是对于侧边栏,我不知道该怎么做。要为每个页面提供不同的内容,现在很容易。我只是在我的页面中添加我想要的内容:

<?php 

$page_title = "Home";
$page_id = "home";
$sidebar = true;

?>

<p>CONTENT HERE FOR THE HOME</p>

结果:http://prntscr.com/a5vegt

您可以在屏幕中看到侧边栏的错误,因为如您所见,我放置了一个变量 $sidebar_content。我希望每个页面都有不同的侧边栏内容。就像:

<?php 

$page_title = "Home";
$page_id = "home";
$sidebar = true;

?>

CONTENT HERE FOR THE HOME

<div class="sidebar-container">
    <p>The sidebar content for the home page</p>
</div>

请问我该怎么办?

【问题讨论】:

  • 为什么不包括一个“sidebar.php”并在那里做你的随机性?即,如果您愿意,可以使用开关盒。
  • 你在哪里定义$sidebar_content??
  • 这就是问题所在,我想定义$sidebar_content,但是不知道怎么做
  • $sidebar_content = rand();
  • 这是什么?我不明白

标签: php


【解决方案1】:

那你为什么不把html放在sidebar.php这样的文件里,然后在你想放这个的地方用include('sidebar.php');呢??

模板.php

<div id="sidebar">
  <?php include('sidebar.php'); ?>
</div>

sidebar.php

$sidebar_dir = 'sidebar_' . $_GET['p'] . '.php'; //e.g. sidebar_home.php
include($sidebar_dir);

【讨论】:

  • 因为这不干净
  • 是的,但是如果我不在主页上?例如,这是将显示在 help.php 页面中的主侧边栏。这是我目前的问题。我需要为每个页面提供不同的侧边栏内容,而不仅仅是一个。动态的东西
  • 你可以让sidebar.php依次包含适当的侧边栏。
  • 您好,如果我的文件在文件夹中,我该怎么办?喜欢:在我的文件夹页面中,帮助文件夹带有 help.php 和 sidebar_help.php?
  • 你的意思是你的侧边栏在help/sidebar_help?你应该创建它的地址...$_GET['p'] . "/sidebar" . "_" . $_GET['p'] . ".php" ...
猜你喜欢
  • 2013-09-28
  • 2021-02-04
  • 2015-03-27
  • 2021-05-29
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 2021-08-31
  • 1970-01-01
相关资源
最近更新 更多