Reputation: 221
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?
Upvotes: 0
Views: 1123
Reputation: 5664
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);
Upvotes: 1