Reputation: 1995
I am trying to include my pages in header.php
file , But I need the list-item to have a class of active
. The Menu is in header.php
and currently looks like this:-
<li class="dropdown">
<a href="#">Help Desk No</a>
<ul class="dropdown-menu">
<li><a title="City Level" href="City-Help.php">City Level</a></li>
<li><a title="State Level" href="State-Help.php">State Level</a></li>
<li><a title="India Level" href="India-Help.php">India Level</a></li>
<li><a title="World Level" href="World-Help.php">World Level</a></li>
</ul>
</li>
what I do :-define variable $page=basename($_SERVER['PHP_SELF']); in header.php
page and for other pages change the variable value according to the page name but it have follow only one sub menu, how it is possible for 2,3,4 sub menus.
<li class="dropdown <?php echo ($page == "City-Help.php" ? "active" : "")?>">
Upvotes: 0
Views: 119
Reputation: 1995
After Searching, Finally I Got the Solution :-
<?php $activePage = basename($_SERVER['PHP_SELF'], ".php"); ?>
<li class="dropdown <?php if($activePage=='City-Help'){echo 'active';}elseif($activePage =='State-Help'){echo 'active';}else{echo 'noactive';} ; ?>">
<a href="#" class="dropdown-toggle" title="Help Desk No">Help Desk No</a>
<ul class="dropdown-menu">
<li><a title="City Level" href="City-Help.php">City Level</a></li>
<li><a title="State Level" href="State-Help.php">State Level</a></li>
</ul>
</li>
Upvotes: 0
Reputation: 442
Edited answer after @Kumar's comment
<li class="dropdown">
<a href="#" class="btn dropdown-toggle <?php echo ($page == "City-Help.php"
OR $page == "State-Help.php" OR $page == "India-Help.php"
OR $page == "World-Help.php") ? "active" : "";?>" data-toggle="dropdown">Help Desk No</a>
<ul class="dropdown-menu">
<li><a title="City Level" href="City-Help.php" >City Level</a></li>
<li><a title="State Level" href="State-Help.php">State Level</a></li>
<li><a title="India Level" href="India-Help.php">India Level</a></li>
<li><a title="World Level" href="World-Help.php">World Level</a></li>
</ul>
</li>
and make sure that you put the php code $page=basename($_SERVER['PHP_SELF']);
before the menu mark-up.
P.S. I added a .btn class to the first <a>
tag, since I guess that you are using bootstrap and in bootstrap, for an ordinary link being active or not has no visual difference.
Upvotes: 1