Looping through sub menu and applying css

still waiting on help!.. :frowning:

Sorry I’ve been at work all day, try this as you shouldn’t need to change anything in it

echo "<style type=\\"text/css\\">
    div.subtree { width: 210px; height: 568px; margin-left: 20px; margin-top: 2px; float: left; overflow: auto; }
    div.subtree ul { color: #0066a4; line-height: 25px; font-weight: bolder; font-size: 12px; font-family: Verdana, Geneva, sans-serif; }
    div.subtree li a { color: #3ca1d3; font-family: Verdana, Geneva, sans-serif; font-size: 11px; text-decoration: none; }
    ul.subchild { list-style: none; margin-left: 15px; }
    ul.subchild li { color: #0066a4;line-height: 20px; margin-left: 15px; margin-bottom: 3px; font-weight: 100; }
    ul.subchild li a.current { color: #f68428; font-weight: 600; border-bottom: 1px dotted #f68428; }
</style>\
\
";

$menu = array(
   array("#", "About me", array(
       "pfolio.php" => "Photography",
       "wfolio.php" => "Web Folio",
       "lfolio.php" => "Logo Folio"
   )),
   array("...", "Rectifier", array(
       "twitter.php"  => "Follow me on twitter",
       "facebook.php" => "Facebook",
       "myspace.php"  => "MySpace",
       "email.php"    => "Email",
       "AtsMake.php"  => "ATS Make"
   ))
);

echo "<div class=\\"subtree\\">\
";
echo "    <ul>\
";

foreach($menu as $menu_item){
    echo "<li class=\\"subchild\\"><a href=\\"".$menu_item[0]."\\"".(substr($_SERVER['PHP_SELF'], 1) === $link ? ' class="current"' : '').">".$menu_item[1]."</a>\
";
   
    if (count($menu_item[2]) > 0){
        echo "<ul class=\\"subchild\\">\
";
        
        foreach($menu_item[2] as $link => $title){
            echo "<li class=\\"subchild\\"><a href=\\"".$link."\\"".(substr($_SERVER['PHP_SELF'], 1) === $link ? ' class="current"' : '').">".$title."</a></li>\
";
        }
        
        echo "</ul>";
    }
    
    echo "</li>";
}

echo "    </ul>";
echo "</div>";

the color still doesn’t change on the click of any page (while on i m the page) :frowning:

I tested it and it worked perfectly for me, the file names in the array have to match those of the ones your linking to otherwise the color wont change

i just copied all of code from ur given post and ran that. nothing of mine. following urs. when i click in photography it goes to the page. r u sure i shld have same code there too or should i change it there or something. tried it again. not working
edit. here is the code again


<?php
echo "<style type=\\"text/css\\">
    div.subtree { width: 210px; height: 568px; margin-left: 20px; margin-top: 2px; float: left; overflow: auto; }
    div.subtree ul { color: #0066a4; line-height: 25px; font-weight: bolder; font-size: 12px; font-family: Verdana, Geneva, sans-serif; }
    div.subtree li a { color: #3ca1d3; font-family: Verdana, Geneva, sans-serif; font-size: 11px; text-decoration: none; }
    ul.subchild { list-style: none; margin-left: 15px; }
    ul.subchild li { color: #0066a4;line-height: 20px; margin-left: 15px; margin-bottom: 3px; font-weight: 100; }
    ul.subchild li a.current { color: #f68428; font-weight: 600; border-bottom: 1px dotted #f68428; }
</style>\
\
";

$menu = array(
   array("#", "About me", array(
       "pfolio.php" => "Photography",
       "wfolio.php" => "Web Folio",
       "lfolio.php" => "Logo Folio"
   )),
   array("...", "Rectifier", array(
       "twitter.php"  => "Follow me on twitter",
       "facebook.php" => "Facebook",
       "myspace.php"  => "MySpace",
       "email.php"    => "Email",
       "AtsMake.php"  => "ATS Make"
   ))
);

echo "<div class=\\"subtree\\">\
";
echo "    <ul>\
";

foreach($menu as $menu_item){
    echo "<li class=\\"subchild\\"><a href=\\"".$menu_item[0]."\\"".(substr($_SERVER['PHP_SELF'], 1) === $link ? ' class="current"' : '').">".$menu_item[1]."</a>\
";
   
    if (count($menu_item[2]) > 0){
        echo "<ul class=\\"subchild\\">\
";
        
        foreach($menu_item[2] as $link => $title){
            echo "<li class=\\"subchild\\"><a href=\\"".$link."\\"".(substr($_SERVER['PHP_SELF'], 1) === $link ? ' class="current"' : '').">".$title."</a></li>\
";
        }
        
        echo "</ul>";
    }
    
    echo "</li>";
}

echo "    </ul>";
echo "</div>"; 
?>

I just noticed something that would cause the main links not to change but as for the sub categories i tested them and they all worked fine for me

Here’s the fixed for loop for the first line which i messed up

foreach($menu as $menu_item){
    echo "<li class=\\"subchild\\"><a href=\\"".$menu_item[0]."\\"".(substr($_SERVER['PHP_SELF'], 1) === $menu_item[0] ? ' class="current"' : '').">".$menu_item[1]."</a>\
"; 
   
    if (count($menu_item[2]) > 0){
        echo "<ul class=\\"subchild\\">\
";
       
        foreach($menu_item[2] as $link => $title){
            echo "<li class=\\"subchild\\"><a href=\\"".$link."\\"".(substr($_SERVER['PHP_SELF'], 1) === $link ? ' class="current"' : '').">".$title."</a></li>\
";
        }
       
        echo "</ul>";
    }
   
    echo "</li>";
} 

sorry i was on vacations i am unable to get the desired result. maybe we aren’t understanding each other.maybe i should come again.
now as the menu is working now making a three dimensional array what i want is like when your on a link (a active link, linke currently i m in fourms its darken), just like that i want the current link to be of color which i gave in the style. till now i haven’t been able to get that.
perhaps if u have a domain, u can upload the file there so i can see if we are on same page on what i m looking for? (don’t get into much detail menu. 3 items would be ok).

Try this

foreach($menu as $menu_item){
    echo "<li class=\\"subchild\\"><a href=\\"".$menu_item[0]."\\"".(preg_match('/'.$menu_item[0].'/', $_SERVER['PHP_SELF']) ? ' class="current"' : '').">".$menu_item[1]."</a>\
"; 
   
    if (count($menu_item[2]) > 0){
        echo "<ul class=\\"subchild\\">\
";
       
        foreach($menu_item[2] as $link => $title){
            echo "<li class=\\"subchild\\"><a href=\\"".$link."\\"".(preg_match('/'.$link.'/', $_SERVER['PHP_SELF']) ? ' class="current"' : '').">".$title."</a></li>\
";
        }
       
        echo "</ul>";
    }
   
    echo "</li>";
}

it worked. thanks! btw what was missing. sorry to ask as i m newbie to php

You were missing nothing, it was i that was missing the error. To put it simply $_SERVER[‘PHP_SELF’] when used in a sub directory returns that directory within the path so instead of searching just for the file name string i changed the simple equal statement to a preg_match statement which searches for the file name within the string and not the whole string itself.

You can learn more about preg_match here http://php.net/manual/en/function.preg-match.php

Hello SL,
i run into a problem, i wanna link two menu to one page. suppose.
About me leads to about.php
About my photography leads to about.php
when i run it, it only highlights one of the menu. i think indexing are unique. maybe that’s is why its happening. how can i fix it?

Unless the pages both use about.php it’s not possible with the current code in the foreach loop, to do this you would need to either setup additional options in the array to check for specific file names or change where photography goes.

both are with extension .php, how can i have additional array of option to hightlight both menu for the same page name. that’s tricky