html - PHP Active Menu link under Submenu -


what i'm trying have main menu link active whilst viewing submenu page using php main menu in includes file. code moment in 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 index page , click link 'jobs.php' on submenu example, 'index.php' above isn't active you'll on 'jobs.php' page still want index.php show active. below doesn't work, new php , i'm sure it's 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 main menu in includes file make easier change in future there's many pages.

best practice write own function , pass sites check. iterate on them , test if 1 of sites shown.

example (tested):

function issite(){     $url = $_server['php_self'];     $sites = func_get_args();     foreach($sites $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"'; 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -