Apply a specific class/id to current page on menu (PHP) -


i have menu this:

<div id="blahblah" style="blahblah"> <a href="http://domain.com/folder/biography"><img style="blahblah" src="blahblahblahblah"></a> <a href="http://domain.com/folder/contacts"><img style="blahblah" src="blahblahblahblah"></a> <a href="http://domain.com/folder/gallery"><img style="blahblah" src="blahblahblahblah"></a> <a href="http://domain.com/folder/dontknow"><img style="blahblah" src="blahblahblahblah"></a> </div> 

i'd have automatically adds class="current" page i'm in. links (as can see in code above) domain.com/folder/biography or domain.com/folder/contacts, without .php/.html, etc.

i tried with:

<div id="blahblah" style="blahblah"> <a <?php if (strpos($_server['php_self'], 'biography')) echo 'class="current"';?> href="http://domain.com/folder/biography"><img style="blahblah" src="blahblahblahblah"></a> <a <?php if (strpos($_server['php_self'], 'contacts')) echo 'class="current"';?> href="http://domain.com/folder/contacts"><img style="blahblah" src="blahblahblahblah"></a> ... ... </div> 

but doesn't work... solution strops seems viable, i'm doing wrong.. :p

you should:

  1. check result of strpos() !== false.
  2. use $_server['request_uri'] rather $_server['php_self'].
  3. wrap code inside function.

something this:

<?php function get_current($name) {   if (strpos($_server['request_uri'], $name) !== false)     echo 'class="current"'; } ?>  <div id="blahblah" style="blahblah">   <a <?php get_current('biography') ?> href="http://domain.com/folder/biography"><img style="blahblah" src="blahblahblahblah"></a>   <a <?php get_current('contacts') ?> href="http://domain.com/folder/contacts"><img style="blahblah" src="blahblahblahblah"></a>   ...   ... </div> 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -