dongzhenju3015 2016-02-20 21:18
浏览 55
已采纳

如何使用php为每个页面添加唯一的侧边栏内容

I'm working on a structure for my site and I have a problem with my sidebar. I would like to have an unique sidebar content for each page, but I don't know how I can do. I created a CSS folder to have my style.css, at the root, a file called index.php and template.php, a pages folder to group all my pages and in this folder, a file called index.php.

The index.php which is at the root has this content (It allows to define each content in each different page):

<?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";

?>

My template file:

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

As you can see, I have a variable $page_content. This allows me to have an unique content for each page, but for the sidebar, I don't know how I can do. To have a different content for each page, it's easy now. I just add what I want in my page like:

<?php 

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

?>

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

And result: http://prntscr.com/a5vegt

You can see an error for the sidebar in the screen, because as you can see, I put a variable $sidebar_content. I would like to have a different sidebar content for each page. Just like:

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

How I can do please?

  • 写回答

1条回答 默认 最新

  • duanganleng0577 2016-02-20 21:24
    关注

    Then why don't you put the html in the file like sidebar.php, and then use include('sidebar.php'); where you want to put this??

    template.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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度