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

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)