Setting "active" status on a drop down menu when the nav.php file is included in.
This post is not as much a question as it is a php novice level solution to a common problem.
I constructed my website using the common method of:
<?php
include "admin.php"; // handles MySQL connection and mysqli prepared stmts. etc.
include "header.php";
include "nav.php"; // nav with drop down menu
?>
<!-- html of the page -->
Everything was fine until I tried to set "active" the appropriate tab and sub-tab. Surely any website designer has run into this issue. I tried googling for answers to no real satisfaction. So I came up with my own.
Let's say a website has 4 tabs (about_us products faq contact) and the deepest drop down is 6 sub-tabs.
I added this line to the bottom of admin.php:
$about = $prod = $faq = $cont = $sub1 = $sub2 = $sub3 = $sub4 = $sub5 = $sub6 = "nothing";
Wasn't sure if I needed to but I add this to the CSS
.nothing { }
The top of each page of the website is:
<?php
include "admin.php";
/** if this page corresponds to the products tab and the fifth sub menu **/
$prod = $sub5 = "active"; // or whatever css uses as the class name
include "header.php";
include "nav.php"; // nav with drop down menu
?>
In nav.php the css class name is echoed in
<div id="centeredmenu">
<nav id="nav">
<ul>
<li class="<?php echo $about; ?>"><a href="about.php">About Us</a>
</li>
<li class="<?php echo $prod; ?>"><a href="/products/index.php">Our Products</a>
<ul>
<li class="<?php echo $sub1; ?>"><a href="/products/index.php">Wedgets</a></li>
<li class="<?php echo $sub2; ?>"><a href="/products/grommets.php">Grommets</a></li>
<li class="<?php echo $sub3; ?>"><a href="/products/some.php">Some</a></li>
<li class="<?php echo $sub4; ?>"><a href="/products/more.php">More</a></li>
<li class="<?php echo $sub5; ?>"><a href="/products/useless.php">Useless</a></li>
<li class="<?php echo $sub6; ?>"><a href="/products/stuff.php">Stuff</a></li>
</ul>
</li>
So there's my novice method of setting active an included nav file.
I would love to see how the pro's would solve this problem. Let's give the checkmark to whichever solution gets the most up votes by their peers. I am sure it won't be mine.