Forum Moderators: coopster

Message Too Old, No Replies

Trying to modify Max Navigation System

         

starpoint

5:45 pm on Jun 18, 2008 (gmt 0)

10+ Year Member



I have downloaded Max's Navigation system and am trying to make a small change to it. The script highlights the active page. But for submenus, it does not highlight the parent item when a submenu item is clicked. I would like the parent item to remain highlighted. It seems like there should be an easy way to do this. I did not see a contact address for the author of this script, so I decided to try and tackle it myself. However I'm a PHP newbie still.

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]

starpoint

7:56 pm on Jun 18, 2008 (gmt 0)

10+ Year Member



Sorry I posted those links. Well, after some fiddling, I managed to get it working. I assigned the ID to the parent link, which has the the name of the parent item, and then used CSS body id to set their active appearance. For example: "Home" is a parent menu item, and its id is set to "Home." "About us" is a parent menu item, so its ID is "About us."

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();

?>

starpoint

4:46 pm on Jun 19, 2008 (gmt 0)

10+ Year Member



Any ideas? Any suggestions would truly help and would be greatly appreciated! Thanks. :)

starpoint

5:55 pm on Jun 19, 2008 (gmt 0)

10+ Year Member



Well, since I did not get any help on here (this board seems kinda dead anyway), I was forced to figure it out for myself. And I did! It really is a wonderful feeling when you accomplish something on your own! I ended up doing this:

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