Forum Moderators: coopster
I know that with CSS, you can assign a body id to each page and then use that to highlight active page items. So I wish to add an "id" to each active submenu item. The "id" should be the same name as that sub menu's parent menu name. I've succeeded in adding the "id" to the link tag on active submenu items, but I am stumped on how to make that id name be the same as the parent item for that submenu. Right now it just outputs a variable (as in id="$mainMenu" I want "$mainMenu" to output an actual value). Here is the script:
<?php
// Main menu items
$mainMenu['Home'] = 'index.php';
$mainMenu['Projects'] = 'projects.php';
$mainMenu['About us'] = 'about.php';
// Sub menu items
$subMenu['Projects']['Product-1'] = 'product1.php';
$subMenu['Projects']['Product-2'] = 'product2.php';
$subMenu['About us']['Staff-1'] = 'staff1.php';
?>
<?php
class maxNavigation{
function showMenu(){
global $mainMenu,$subMenu;
$actualPage = $_SERVER['PHP_SELF'];
$actualPath = $_SERVER['REQUEST_URI'];
$actualPageName = basename($actualPage);
//echo $page;
//echo "$actualPage <br/> $actualPath";
$actMenu = '';
foreach ($mainMenu as $menu => $link) {
if ($link == $actualPageName) $actMenu = $menu;
if (isset($subMenu[$menu])){
foreach ($subMenu[$menu] as $menuSub => $linkSub) {
if ($linkSub == $actualPageName) $actMenu = $menu;
}
}
}
// Now display the menu
foreach ($mainMenu as $menu => $link) {
$class = ' class="mainMenuLink" ';
if ($actualPageName == $link) $class=' class="mainMenuLinkSelected" ';
echo '<a'.$class.'href="'.$link.'">'.$menu.'</a>';
if ( ($actMenu == $menu) && (isset($subMenu[$menu])) ){
foreach ($subMenu[$menu] as $menuSub => $linkSub) {
$class = ' class="subMenuLink" ';
// THIS IS WHERE I ADDED THE MODIFIED CODE, WHERE IT SAYS ID=$MAIN
MENU
if ($actualPageName == $linkSub) $class=' class="subMenuLinkSelected" id="$mainMenu" ';
echo '<a'.$class.'href="'.$linkSub.'">'.$menuSub.'</a>';
}
}
}
}
}
$navi = new maxNavigation();
$navi->showMenu();
?>
You can see it working at <snip>
Click on "projects" and then click "product 2" from the submenu. Then view the source, and you'll see the id added to the link, but it just shows id="$mainMenu" when I want it to be "Projects" - the name of the parent menu which was defined at the top of the script.
Thanks for any points or suggestions in the right direction. What am I doing wrong?
I am not planning to make this script my own, this is not going to be for commercial use. Here's the original author of the script: <snip>
[edited by: dreamcatcher at 6:16 pm (utc) on June 18, 2008]
[edit reason] No urls please! [/edit]
The only problem is, spaces in the CSS id name aren't recognized. How do I set it so that it gets rid of spaces in the ID name? Here's my code so far. I want to get rid of the spaces in "About us" ONLY when it is called as the ID name. I tried using the string replace, but it applies the change to the menu item names themselves. I want it only in the ID of the link. Thanks for any help!
<?php
// Main menu items
$mainMenu['Home'] = 'index.php';
$mainMenu['Projects'] = 'projects.php';
$mainMenu['About us'] = 'about.php';
// Sub menu items
$subMenu['Projects']['Product-1'] = 'product1.php';
$subMenu['Projects']['Product-2'] = 'product2.php';
$subMenu['About us']['Staff-1'] = 'staff1.php';
?>
<?php
class maxNavigation{
function showMenu(){
global $mainMenu,$subMenu;
$actualPage = $_SERVER['PHP_SELF'];
$actualPath = $_SERVER['REQUEST_URI'];
$actualPageName = basename($actualPage);
//echo $page;
//echo "$actualPage <br/> $actualPath";
$actMenu = '';
foreach ($mainMenu as $menu => $link) {
if ($link == $actualPageName) $actMenu = $menu;
if (isset($subMenu[$menu])){
foreach ($subMenu[$menu] as $menuSub => $linkSub) {
if ($linkSub == $actualPageName) $actMenu = $menu;
}
}
}
// Now display the menu --
foreach ($mainMenu as $menu => $link) {
//THIS IS WHERE I WANT THE ID TO BE RID OF THE SPACES
$class = ' class="mainMenuLink" id="'.$menu.'" ';
if ($actualPageName == $link) $class=' class="mainMenuLinkSelected" id="'.$menu.'" ';
echo '<a'.$class.'href="'.$link.'">'.$menu.'</a>';
if ( ($actMenu == $menu) && (isset($subMenu[$menu])) ){
foreach ($subMenu[$menu] as $menuSub => $linkSub) {
$class = ' class="subMenuLink" ';
if ($actualPageName == $linkSub) $class=' class="subMenuLinkSelected" ';
echo '<a'.$class.'href="'.$linkSub.'">'.$menuSub.'</a>';
}
}
}
}
}
$navi = new maxNavigation();
$navi->showMenu();
?>
<?php
// Main menu items
$mainMenu['Home'] = 'index';
$mainMenu['Projects'] = 'projects';
$mainMenu['About us'] = 'about';
// Sub menu items
$subMenu['Projects']['Product-1'] = 'product1';
$subMenu['Projects']['Product-2'] = 'product2';
$subMenu['About us']['Staff-1'] = 'staff1';
?>
<?php
class maxNavigation{
function showMenu(){
global $mainMenu,$subMenu;
$suffix_id='.php';
$actualPage = $_SERVER['PHP_SELF'];
$actualPath = $_SERVER['REQUEST_URI'];
$actualPageName = basename($actualPage , $suffix_id);
//echo $page;
//echo "$actualPage <br/> $actualPath";
$actMenu = '';
foreach ($mainMenu as $menu => $link) {
if ($link == $actualPageName) $actMenu = $menu;
if (isset($subMenu[$menu])){
foreach ($subMenu[$menu] as $menuSub => $linkSub) {
if ($linkSub == $actualPageName) $actMenu = $menu;
}
}
}
// Now display the menu
foreach ($mainMenu as $menu => $link) {
$class = ' class="mainMenuLink" id="'.$link.'" ';
if ($actualPageName == $link) $class=' class="mainMenuLinkSelected" id="'.$link.'" ';
echo '<a'.$class.'href="'.$link , $suffix_id.'">'.$menu.'</a>';
if ( ($actMenu == $menu) && (isset($subMenu[$menu])) ){
foreach ($subMenu[$menu] as $menuSub => $linkSub) {
$class = ' class="subMenuLink" ';
if ($actualPageName == $linkSub) $class=' class="subMenuLinkSelected" ';
echo '<a'.$class.'href="'.$linkSub , $suffix_id.'">'.$menuSub.'</a>';
}
}
}
}
}
$navi = new maxNavigation();
$navi->showMenu();
?>
Works beautifully now. So thanks for not helping me, it worked out well in the end because I learned some things for myself. :-)
Not like anyone is reading this anyway. Oh well.