Hi
Right now I am using the swich command in one of my scripts but am wondering if it's smart to use that command in this particular situation. Maybe some of you have a better/smarter idea...
I have a file (menu.php) that loads a menu with links for the user to click on. The menu choice that is the page that the user is browsing right now should be in a different colour.
The complete page, including the menu is reloaded when the user clicks a link. I am not using frames.
In a stylesheet I have defined two classes; one for a normal link and one for an active link in the menu.
There are four menu links. In menu.php I have assigned a variable to each choice. So
Link 1 = $link1
Link 2 = $link2, and so on
Lets say the user clicks on link 2, that link should be highlighted. To do this variable $link2 is assigned the value of "menu_active" (menu_active being the name of the stylesheet class that displays an active link). The other link-variables are assigned the value of the not-active-link.
This way the right class is used for the active link.
The menu looks like this:
<?php
echo "<td class=\"$link1\">";
echo "Choice 1";
echo "</td>";
echo "<td class=\"$link2\">"; // should be "menu_active" in this example
echo "Choice 2";
echo "</td>";
echo "<td class=\"$link3\">";
echo "Choice 3";
echo "</td>";
echo "<td class=\"$link4\">";
echo "Choice 4";
echo "</td>";
?>
When the user clicks a link the link number is stored in a cookie $menuC, which I use when the menu is reloaded after the click.
The link number that corresponds with $menuC is then given the value of "menu_active".
To evaluate $menuC to see what choice was clicked I use the switch command below:
switch ($menuC){
case 1:
$link1 = menu_active;
case 2:
$link2 = menu_active;
case 3:
$link3 = menu_active;
case 4:
$link4 = menu_active;
break;
With only four menu choices it's easy but what if i have to add many more later...?
I think there must be a much easier and smarter way to do this...
Anyone? 🙂