Jump to content

Advice on creating "current item highlighted" menu


Wuhtzu

Recommended Posts

Hey

 

I'm building a small site which consist of a few menu items each "linked" to individual scripts like this:

 

Home -> index.php

T-Shirts -> tshirts.php

Contact -> contact.php

(Just to mention a few of them)

 

With mod_rewrite the URLs look like this:

 

mysite.com -> mysite.com/some/other/location/index.php

mysite.com/tshirts -> mysite.com/some/other/location/tshirts.php

mysite.com/contact -> mysite.com/some/other/location/contact.php

 

What I would like some advice on is the neatest / smartest way to create a menu which have "current category/menu item" highlighted... So when you are viewing mysite.com/tshirts the T-Shirt menu item is highlighted.

 

How should I use PHP to determine which item to highlight in this case?

 

If the site was build around a single file index.php which took a $_GET parameter page it would be simple since you would simply base your highlighting on $_GET['page'] but I'm not quite sure how I should do this...

 

Please give me your thoughts :)

 

 

Thanks alot

Wuhtzu

Link to comment
Share on other sites

This is what i would do:

 

put each link in an array

 

<?php
$links = array (
'home'  =>  'index.php',
'other'  =>  'index.php?page=other'
);
?>

 

Do that for each page, then run a for loop where you want the links to be

 

<?php
while ( list($title,$url) = each($links) )
{
  if ($_GET['page'] == $title) // if its the current page, make it non-clickable
  {
    echo "{$title}<br />";
  }
  else // otherwise, make it clickable
  {
    echo "<a href={$url}>{$title}</a><br />";
  }
}
?>

Link to comment
Share on other sites

^ then do this

 

<?php
// Get the current script name. it will be stored in $pfile
// example.. yoursite.com/moneybaby.php would output "moneybaby.php"
//
$file = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $file);
$pfile = $break[count($break) - 1];


while ( list($title,$url) = each($links) )
{
  if ($_GET['page'] == $pfile) // if its the current page, make it non-clickable
  {
    echo "{$title}<br />";
  }
  else // otherwise, make it clickable
  {
    echo "<a href={$url}>{$title}</a><br />";
  }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.