My PHP site has header,body,footer files that are included into the INDEX.PHP page.
On the header.php file I have a menu like this
<div id="bottomrow">
<div class="pad">
<ul class="menu left">
<li class="first <?=$current_home?>"><a href="/"><em>Home</em></a></li>
<li class="users drop <?=$current_users?>"><a href=""><em>Users</em></a><span class="drop"> </span>
<ul id="moreheader">
<li></li>
<li></li>
</ul>
</li>
<li class="<?=$current_forum?>"><a href=""><em>Forums</em></a></li>
<li class="drop <?=$current_more?>"><a href="/moreheader"><em>More</em></a><span class="drop"> </span>
<ul id="moreheader">
<li><a href=""><em>Widgets</em></a></li>
<li><a href=""><em>News</em></a></li>
<li><a href=""><em>Promote</em></a></li>
<li><a href=""><em>Development</em></a></li>
<li><a href=""><em>Bookmarks</em></a></li>
<li><a href=""><em>About</em></a></li>
</ul>
</li>
<li class="moneytabmenu <?=$current_money?>"><a href="/moneytabmenu"><em>Money:<span class="moneytabmenu-total">$0.00</span></em></a></li>
</ul>
<ul class="menu right">
<li class="drop myaccount <?=$current_myaccount?>"><a href="" class="first"><img class="avatar" src="http://gravatar.com/avatar.php?gravatar_id=7ab1baf18a91ab4055923c5fd01d68a2&rating=pg&size=80&default=" height="19" width="19" alt="you" /><em>My
Account</em></a><span class="drop"> </span>
<ul id="myaccount">
<li><a href=""><em>Dashboard</em></a></li>
<li><a href=""><em>Account Settings</em></a></li>
<li><a href=""><em>Settings</em></a></li>
</ul>
</li>
<li class="drop"><a href=""><em>Mail</em></a><span class="drop"> </span>
<ul id="mailboxheader">
<li><a href=""><em>InBox</em></a></li>
<li><a href=""><em>SentBox</em></a></li>
<li><a href=""><em>Trash</em></a></li>
<li><a href=""><em>Post Bulletin</em></a></li>
<li><a href=""><em>View Bulletins</em></a></li>
</ul>
</li>
<li class="drop <?=$current_more?>"><a href=""><em>More</em></a><span class="drop"> </span>
<ul id="moreheader">
<li><a href=""><em>Widgets</em></a></li>
<li><a href=""><em>News</em></a></li>
<li><a href=""><em>Promote</em></a></li>
<li><a href=""><em>Development</em></a></li>
<li><a href=""><em>Bookmarks</em></a></li>
<li><a href=""><em>About</em></a></li>
</ul>
</li>
</ul>
</div>
</div>
<!-- END div#bottomrow -->
</div>
The menu is similar to above, the above menu is not completed but you can see how it is set up, there are
If a user is on any of the pages in a sub-menu, it should add the “current” css class to the parent menu item.
Below is my index.php page where I can determine which page I am on using $_GET for the variable index.php?p=PAGE-NAME
I posted a question earliar similar to this but it was before I did the code below and it didnt get much response, the response it did get all mentioned using arrays but that was beofre they new that I had sub-menus.
So does anyone see a better way to add the css class “current” to a list item above based on which page I am on?
Code on INDEX.php page that processes which menu item should be highlighted
//set variables for menu item to appear as being SELECTED
$p = $_GET['p'];
$current_home = '';
$current_users = '';
$current_forum = '';
$current_more = '';
$current_money = '';
$current_myaccount = '';
$current_mail = '';
//if home then highlight home menu
if($p === 'home'){
$current_home = 'current';
}
if($p === 'users.online' || $p === 'users.location' || $p === 'users.featured' || $p === 'users.new' || $p === 'users.browse' || $p === 'users.search' $p === 'users.staff'){
$current_users = 'current';
}
if($p === 'forum'){
$current_forum = 'current';
}
if($p === 'widgets' || $p === 'news' || $p === 'promote' || $p === 'development' || $p === 'bookmarks' || $p === 'about'){
$current_more = 'current';
}
if($p === '=account.money' || $p === 'account.store' || $p === 'account.lottery' || $p === 'users.top.money'){
$current_money = 'current';
}
if($p === 'account'){
$current_myaccount = 'current';
}
if($p === 'mail.inbox' || $p === '=mail.sentbox' || $p === 'mail.trash' || $p === 'bulletins.post' || $p === 'bulletins.my' || $p === 'bulletins'){
$current_mail = 'current';
}
I would recommend making the whole thing more data driven. Create a multi-dimensional array of your menu options and then iterate through the array, outputting the correct HTML.
Here’s how I would do it (this hasn’t been tested, but the idea is what I’m trying to convey):