Reputation: 17
What I'm trying to do is to have the Main Menu link active whilst viewing a Submenu page using PHP as my Main Menu is in an includes file. My code for the moment in the includes file is:
<p class="menulinks">
<strong class="hide">Main Menu:</strong>
<a <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="menulink active"';?> class="menulink" href="index.php">Home</a><span class="hide"> | </span>
<a <?php if (strpos($_SERVER['PHP_SELF'], 'food.php')) echo 'class="menulink active"';?> class="menulink" href="food.php">Food</a><span class="hide"> | </span>
<a <?php if (strpos($_SERVER['PHP_SELF'], 'drinks.php')) echo 'class="menulink active"';?> class="menulink" href="drinks.php">Drink</a><span class="hide"> | </span>
<a <?php if (strpos($_SERVER['PHP_SELF'], 'gallery.php')) echo 'class="menulink active"';?> class="menulink" href="gallery.php">Gallery</a><span class="hide"> | </span>
<a <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="menulink active"';?> class="menulink" href="contact.php">Contact</a>
</p>
When I'm on the Index page and I click the link 'jobs.php' on the submenu for example, 'index.php' above isn't active as you'll be on the 'jobs.php' page but I still want index.php to show as active. Something like this below but it doesn't work, new to PHP and I'm sure it's something basic:
<a <?php if (strpos($_SERVER['PHP_SELF'], 'index.php jobs.php')) echo 'class="menulink active"';?> class="menulink" href="index.php">Home</a>
The main reason the Main Menu is in an includes file is to make it easier to change in the future as there's so many pages.
Upvotes: 1
Views: 1256
Reputation: 796
I've rewritten wumm's code and added few things.
<?php
function is_page() {
$currentPage = $_SERVER['PHP_SELF'];
$pages = func_get_args();
foreach($pages as $page) {
if(strpos($currentPage, $page)) return true;
}
return false;
}
$selected = ' active';
?>
<p class="menulinks">
<strong class="hide">Main Menu:</strong>
<a class="menulink<?php echo is_page('index.php', 'home.php', 'homepage.php') ? $selected : '';?>" href="index.php">Home</a><span class="hide"> | </span>
<a class="menulink<?php echo is_page('food.php') ? $selected : '';?>" href="food.php">Food</a><span class="hide"> | </span>
<a class="menulink<?php echo is_page('drinks.php') ? $selected : '';?>" href="drinks.php">Drink</a><span class="hide"> | </span>
<a class="menulink<?php echo is_page('gallery.php') ? $selected : '';?>" href="gallery.php">Gallery</a><span class="hide"> | </span>
<a class="menulink<?php echo is_page('contact.php') ? $selected : '';?>" href="contact.php">Contact</a>
</p>
Upvotes: 0
Reputation: 14875
Best practice would be to write your own function and pass the sites to check. Then iterate over them and test if one of the sites is currently shown.
Example (tested):
function isSite(){
$url = $_SERVER['PHP_SELF'];
$sites = func_get_args();
foreach($sites as $site){
if(strpos($url, $site)) return true;
}
return false;
}
So
if (strpos($_SERVER['PHP_SELF'], 'index.php jobs.php')) echo 'class="menulink active"';
would become
if (isSite('index.php','jobs.php')) echo 'class="menulink active"';
Or:
if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="menulink active"';
becomes
if (isSite('index.php')) echo 'class="menulink active"';
Upvotes: 2