Wuhtzu Posted October 21, 2007 Share Posted October 21, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/74152-advice-on-creating-current-item-highlighted-menu/ Share on other sites More sharing options...
Wes1890 Posted October 21, 2007 Share Posted October 21, 2007 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 />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74152-advice-on-creating-current-item-highlighted-menu/#findComment-374475 Share on other sites More sharing options...
Wuhtzu Posted October 21, 2007 Author Share Posted October 21, 2007 That is exactly what I would do _if_ the site was build around index.php which took page as a get parameter, but as I said that is not the case Quote Link to comment https://forums.phpfreaks.com/topic/74152-advice-on-creating-current-item-highlighted-menu/#findComment-374519 Share on other sites More sharing options...
Wes1890 Posted October 21, 2007 Share Posted October 21, 2007 ^ 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 />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74152-advice-on-creating-current-item-highlighted-menu/#findComment-374520 Share on other sites More sharing options...
Wuhtzu Posted October 21, 2007 Author Share Posted October 21, 2007 Yup that is definitely an option... you don't think it's "not neat" to use the script name? Quote Link to comment https://forums.phpfreaks.com/topic/74152-advice-on-creating-current-item-highlighted-menu/#findComment-374524 Share on other sites More sharing options...
Wes1890 Posted October 21, 2007 Share Posted October 21, 2007 ^ I've always done that.. same exact thing, works like a charm Although you could go through and set a pagename variable in each .php page you have. But thats no fun, make the PHP do all the work and just use the scripts name Quote Link to comment https://forums.phpfreaks.com/topic/74152-advice-on-creating-current-item-highlighted-menu/#findComment-374526 Share on other sites More sharing options...
Wuhtzu Posted October 21, 2007 Author Share Posted October 21, 2007 I'll give it a shot and see if I like it. Thank you wes1890 Quote Link to comment https://forums.phpfreaks.com/topic/74152-advice-on-creating-current-item-highlighted-menu/#findComment-374715 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.