Issue
I’m trying to clean up my code in my php web pages by including files to consolidate each page’s code’s like so:
File: head.php
<!DOCTYPE html>
<html>
<head>
<title> *Dynamic Page Title* </title>
<meta name"description" "Dynamic Description">
<link href="#">
</head>
File: footer.php
<footer>
<ul>
<a href="#"><li>Home</li></a>
<a href="#"><li>Page 1</li></a>
<a href="#"><li>Page 2</li></a>
</ul>
</footer>
<script src="#">
</body>
</html>
File: navbar.php
<header class="navbar navbar-default navbar-static-top">
<div class="container-fluid">
<div class="navbar-header">
<a href="#" class="navbar-brand"></a>
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><i class="fa fa-bars"></i></button>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="index">Home</a></li>
<li><a href="#">Page 1</a></li>
<li><a href="#">Page 2</a></li>
<li><a href="#">FAQ</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</div>
</div>
</header>
File: index.php
<?php include('head.php'); ?>
<?php include('nav-bar.php'); ?>
<body>
Index Page, Home has an active class
<?php include('footer.php'); ?>
File: Page-1.php
<?php include('head.php'); ?>
<?php include('nav-bar.php'); ?>
<body>
Index Page, Page 1 in nav-bar has an active class
<?php include('footer.php'); ?>
You guys get the main idea. I would like tips on how to add active classes in different pages in the nav-bar, and how to add different titles and meta tags to each page.
Any help?
Solution
You can define variables in your files:
index.php
:
<?php $title = 'Home'; ?>
<?php $metaTags = 'tag1 tag2'; ?>
<?php $currentPage = 'index'; ?>
<?php require_once(__DIR__.'/head.php'); ?>
<?php require_once(__DIR__.'/php'); ?>
<body>
<h1>Index Page, Home has an active class</h1>
<?php require_once(__DIR__.'/footer.php'); ?>
</body>
Then use these variables in the included file:
head.php
:
<!DOCTYPE html>
<html>
<head>
<title><?php echo($title); ?></title>
<meta name"description" "<?php echo($metaTags); ?>"><!-- note that this code is wrong -->
<link href="#">
</head>
navbar.php
:
<header class="navbar navbar-default navbar-static-top">
<div class="container-fluid">
<div class="navbar-header">
<a href="#" class="navbar-brand"></a>
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><i class="fa fa-bars"></i></button>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li <?php if ($currentPage === 'Home') {echo 'class="active"';} ?>><a href="index">Home</a></li>
<li><a href="#">Page 1</a></li>
<li><a href="#">Page 2</a></li>
<li><a href="#">FAQ</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</div>
</div>
</header>
In this last part, it’s easier to use an array instead of repeating the same code for each line:
<ul class="nav navbar-nav navbar-right">
<?php
// Define each name associated with an URL
$urls = array(
'Home' => '/',
'Page 1' => '/page1',
// …
);
foreach ($urls as $name => $url) {
print '<li '.(($currentPage === $name) ? ' class="active" ': '').
'><a href="'.$url.'">'.$name.'</a></li>';
}
?>
</ul>
Now you can copy-paste index.php
as a new file, and add to your navbar, a link will be added on every page.
Answered By – A.L
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0