delphi123 Posted January 16, 2008 Share Posted January 16, 2008 Hi, I'm needing to use a server command to set a menu according to which page it's on. Ie so if you're on the home page, set the 'home' tab to highlighted, if you're on the news page, highlight that tabs and so on. (much the same way as the forum tabs at the top are formatted). I've got seven menu options, so I'm thinking of having seven variables and setting them on and off as required. I can then have a php echo in each tab class setting to class="tab_on" or class="tab_off". This'll require a fair bit of code, can anyone think of a more elegant way of achieving this? Quote Link to comment https://forums.phpfreaks.com/topic/86329-ideas-for-menu-tab-settings/ Share on other sites More sharing options...
delphi123 Posted January 16, 2008 Author Share Posted January 16, 2008 Ok I tried using a switch statement like this: <?php // Menu options for active page $menu_home = ""; $menu_news = ""; $menu_products = ""; $menu_tutorials = ""; $menu_charts = ""; $menu_community = ""; $menu_support = ""; switch ($activepage) { case 'http://www.site.net/': $menu_home = "active"; break; case 'http://www.site.net/content/news/': $menu_news = "active"; break; default: $menu_home = "active"; } ?> However I've realised the problem is it needs to be cleverer, if for example I look at a sub page like these: http://www.site.net/content/news/ http://www.site.net/content/news/news1/index.htm http://www.site.net/content/news/news2/index.htm it won't be picked up - is there a way to say 'if url contains this part http://www.site.net/content/news/ then set to x'? Quote Link to comment https://forums.phpfreaks.com/topic/86329-ideas-for-menu-tab-settings/#findComment-441102 Share on other sites More sharing options...
Wuhtzu Posted January 16, 2008 Share Posted January 16, 2008 What you have explained is pretty much the way to do it. 1: Get your menu options from somewhere - database or predefined array 2: Get the page you are on (e.g. news) 3: Loop through all your menu options and echo something like: "<a href=$page>$text</a>" 4: If the menu $page == $current_page echo "<a id=current href=$page>$text</a>" Terrible syntax, but it's just the general idea - just as you presented it Quote Link to comment https://forums.phpfreaks.com/topic/86329-ideas-for-menu-tab-settings/#findComment-441105 Share on other sites More sharing options...
delphi123 Posted January 16, 2008 Author Share Posted January 16, 2008 hmm but the sticking point is how do i 'get the page I'm on'? So I can have a variable saying: $home = www.site.com but what if they come to www.site.com/index.htm or http://www.site.com etc etc... How do people normally do this? Quote Link to comment https://forums.phpfreaks.com/topic/86329-ideas-for-menu-tab-settings/#findComment-441115 Share on other sites More sharing options...
Wuhtzu Posted January 16, 2008 Share Posted January 16, 2008 It depends on the "structure" or "design pattern" of your project. A common way of paginating a site is to pass a variable in the url like www.site.com/index.php?page=news and then include pages according to the value of $_GET['page']. With this concept you could easily get the "current page" from $_GET['page']. Another common design pattern is the MVC where you typically have url's like this: www.site.com/controller/action/aditional_parameters which is basically www.site.com/index.php?controller=xx&action=yy&aditional_parameters=zz rewritten with mod_rewrite. Here you will probably get the name of the controller which could be news and then highlight according to that... So no matter if it www.site.com/news/ or www.site.com/news/view/235 both would highlight news. It's actually a very complicated question... This is what grounded my PHP learning / development to a halt because it was the point where I had to go from making small standalone scripts (though they could be complex) to make larger projects according to some design pattern:S Maybe this can be of help: http://www.phpfreaks.com/forums/index.php/topic,164453.msg721924.html#msg721924 Quote Link to comment https://forums.phpfreaks.com/topic/86329-ideas-for-menu-tab-settings/#findComment-441135 Share on other sites More sharing options...
revraz Posted January 16, 2008 Share Posted January 16, 2008 Can't you compare to $PHPSELF? Quote Link to comment https://forums.phpfreaks.com/topic/86329-ideas-for-menu-tab-settings/#findComment-441145 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.