Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/86329-ideas-for-menu-tab-settings/
Share on other sites

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'?

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 :)

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

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.