Looping through sub menu and applying css

hey folks, here i made a menu. now i want some fields to be child of main menu like under about me section there are 4 links, which i want to intend. and which is currently active should have class of id current. i have made it so far but i don’t know how to apply style to submenu of main menu. can i get some help? here is my code with css


<html>
	<head>
		<title>Menu</title>
	<style type="text/css">
	ul parent, ul child{margin:0}
	.parent li{margin-bottom:5px; list-style:none; text-decoration:none; color:maroon;}
	.child li{margin-left:10px;list-style:none;}/*sub menu of main menu*/
	.child li a#current{color:pink;}/*the active link will have this class*/
	</style>
	</head>
	
<body>
<?php
$menu = array('Home',
			'about.php'=>'About me',
			'studio.php'=>'Photo Studio',
			'web.php'=>'Web Designing',
			'arts.php'=>'Visual Arts',
			'logo.php'=>'Logo Designing',
			'#'=>'Contact',
			'facebook.php'=>'Facebook',
			'twitter.php'=>'Twitter',
			'linkdIn.php'=>'LinkdIn');
?>
<ul class="parent">
<?php 
foreach($menu as $menupointer => $newlink){
echo "<a href=\\"$menupointer\\"><li>$newlink</li></a>";
}
?>
</ul>
also i have a problem, when i give same url or hash to a menu name. it disappear. i think its unique and only latter one is picked. how can i fix that. as i don't want any link on parent which have sub menu.
thanks in advance
</body>
</html>

Try this, yes its a 3 dimensional array

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

Try this, I’ve tested it and it appears to be working fine

<html>
<head>
<title>Menu</title>
<style type="text/css">
<!--
    ul parent, ul child{margin:0}
    .parent li{margin-bottom:5px; list-style:none; text-decoration:none; color:maroon;}
    .child li{margin-left:10px;list-style:none;}/*sub menu of main menu*/
    .child li a#current{color:pink;}/*the active link will have this class*/
-->
</style>
</head>
<body>
<?php
$menu = array(
    array('#', 'Home', array(
        'about.php'   => 'About me',
        'studio.php'  => 'Photo Studio',
        'web.php'     => 'Web Designing',
        'arts.php'    => 'Visual Arts',
        'logo.php'    => 'Logo Designing'
    )),
    array('#', 'Contact', array(
        'facebook.php'  => 'Facebook',
        'twitter.php'   => 'Twitter',
        'linkdIn.php'   => 'LinkdIn'
    ))
);
?>
<ul class="parent">
<?php
foreach($menu as $menu_item){
    echo "<li class=\\"parent\\"><a href=\\"".$menu_item[0]."\\">".$menu_item[1]."</a>";
    
    if (count($menu_item[2]) > 0){
        echo "<ul class=\\"child\\">";
        
        foreach($menu_item[2] as $link => $title){
            echo "<li class=\\"child\\"><a href=\\"".$link."\\">".$title."</a></li>";
        }
        
        echo "</ul>";
    }
    
    echo "</li>";
}
?>
</ul>

</body>
</html>

I’m… sort of confused, here. Which of the entries are the children?

I -think- I understand what you mean… someone’s going to come along and point out a better way to do this, but…


<?php 
foreach($menu as $menupointer => $newlink){
echo "<a href=\\"$menupointer\\"><li>$newlink</li></a>";
if(array_pop(explode('/',$_SERVER['PHP_SELF'])) === $menupointer) {
 echo "<ul>";
 foreach($children[$menupointer] as $childpointer => $childlink) {
   echo "<li><a href=\\"$childpointer\\">$childlink</a></li>";
 }
 echo "</ul>";
} 
}
?>

You should be able to modify that into what you need.

Is it 3 dimensional array? Though one thing is missing. How can I assign the current Id to current value?

thanks, while i try this. can u help me little bit break it down. this line (substr($_SERVER[‘PHP_SELF’], 1) === $link ? ’ id=“current”’ : ‘’)

Basically what im saying there is the same as saying

if (substr($_SERVER['PHP_SELF'], 1) === $link){
    // Something
} else {
    // Something else
}

The substr function moves the pointer ahead one spot as $_SERVER[‘PHP_SELF’] keeps the forward slash / at the beginning of the string. Hope that explains it bud

sgtlegend i get error on
foreach($menu_item[2] as $link => $title)
the error says that invalid arguement supplied for foreach()

Did you create the $menu array as mentioned earlier in this thread? (The solution provided by SgtLegend expects this array to exist).

yes its the basis of the array container. in other words variable with contain arrays

I suspect you’ll need to show your full code (pertaining to this issue) - it’s not possible to debug without seeing exactly what you have used. :slight_smile:

Don’t forget to use code highlight tags. :wink:

Browsers that need the contents of style or script tags to be surrounded by comment text to not accidentally display them are no longer in use by anyone sane. Indeed, I haven’t used that kind of markup since 1997 or so.

Unless you structure the array the way i did the array wont work, it needs the values to be in the order i set them to

Browsers that need the contents of style or script tags to be surrounded by comment text to not accidentally display them are no longer in use by anyone sane. Indeed, I haven’t used that kind of markup since 1997 or so.

Force of habit, i only ever do it so i can read the CSS

here is it


<?php
$menu = array(
				
				"#"=>"About me",
				"pfolio.php"=>"Photography",
				"wfolio.php"=>"Web Folio",
				"lfolio.php"=>"Logo Folio",
				
				"..."=>"Rectifier",
				"twitter.php"=>"Follow me on twitter",
				"facebook.php"=>"Facebook",
				"myspace.php"=>"MySpace",
				"email.php"=>"Email",
				"AtsMake.php"=>"ATS Make",
				
	
				);
?>
<?php
foreach($menu as $menu_item){
    echo "<li class=\\"parent\\"><a href=\\"".$menu_item[0]."\\">".$menu_item[1]."</a>";
    
    if (count($menu_item[2]) > 0){
        echo "<ul class=\\"child\\">";
		
        
       [B] foreach($menu_item[2] as $link => $title){[/B]
            echo "<li class=\\"child\\"><a href=\\"".$link."\\"".(substr($_SERVER['PHP_SELF'], 1) === $link ? ' id="current"' : '').">".$title."</a></li>";
        }
        
        echo "</ul>";
    }
    
    echo "</li>";
}  
?>

the bold line has the error. here i couldn’t make a good css style to demonstrate but still, it tell wh’s wrong with php
edit: also there is another problem like in this example i have two main headings. which i don’t wanna give a link for coz its just a parent o child links. if i give it # both times. only the preceding will work.

The array you posted is nothing like the one i posted, here is the correct version

$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"
    ))
);

Sorry about before post. i forgot the edit the arrays to make them like u told me, its still not printing anything on screen. i seen the source code and here it is


<html>
	<head>
	<title></title>
<style type="text/css">
ul li parent{color:#000;}
</style>
    </head>

<body> 
<li class="parent"><a href="Array"></a></li><li class="parent"><a href="Array"></a></li></body>
</html>

and here is the php


<?php
$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"
   ))
);
?>
<?php
foreach($menu as $menu_item){
    echo "<li class=\\"parent\\"><a href=\\"".$menu_item[0]."\\">".$menu_item[1]."</a>";
    
    if (count($menu_item[2]) > 0){
        echo "<ul class=\\"child\\">";        
        foreach($menu_item[2] as $link => $title){
            echo "<li class=\\"child\\"><a href=\\"".$link."\\"".(substr($_SERVER['PHP_SELF'], 1) === $link ? ' id="current"' : '').">".$title."</a></li>";
        }       
        echo "</ul>";
    }    
    echo "</li>";
}  
?>

its not working

Your array still had one more issue, you were trying to use the link as a key for the title instead of using a , “coma” to break the values apart

Change

array("#" => "About me", array(

To

array("#", "About me", array(

Change

array("..." => "Rectifier", array(

To

array("...", "Rectifier", array(

thanks SL, sorry that codes just got me confused. though a little problem left. the links i am currently active on has different class/color. now what happen is when i give

echo "<li class=\\"child\\"><a href=\\"".$link."\\">".$title."</a></li>";

a href class of current. every link of the page active or not, get that style. how can i make it for link currently active and not for all.

Simply make the first line of the foreach loop

foreach($menu as $menu_item){
    echo "<li class=\\"parent\\"><a href=\\"".$menu_item[0]."\\"".(substr($_SERVER['PHP_SELF'], 1) === $menu_item[0] ? ' id="current"' : '').">".$menu_item[1]."</a>";

In this line change id=“current” to what it needs to be

its not working :(, here is the code


div.subtree{width:150px; 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;}
.subchild li{ color:#0066a4;line-height:20px; margin-left:15px; margin-bottom:3px; font-weight:100;}
.subchild a.current{color:#f68428; font-weight:600; border-bottom:1px dotted #f68428;}


<ul class="subtree">
<?php
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 clas href=\\"".$link."\\">".$title."</a></li>";
        }     
        echo "</ul>";
    }   
    echo "</li>";
}
?>
</ul>

why isn’t working