Jump to content

Array Menu with Child Items


Ducimus

Recommended Posts

Ok, I am a cooding noob and have taken over working on this website for a friend. it was originally coded in php and features a horizontal menu built from an array.

I would now like to add child items.

I have created a simple page with all the existing php and css explainging what i'm trying to accomplish. see: www.jestley.com/phpquestion.html

I validated my css at it works, now i just have to figure out how to get the second level into the array.

please any help would be greatly appreciated.

TIA

Link to comment
https://forums.phpfreaks.com/topic/181249-array-menu-with-child-items/
Share on other sites

Simple.

 

$Menu['Valo Services'] = array('Valo Home Watch', 'Value Home Renovations', 'Valo Outdoor Living Spaces');

$Menu['Contact Valo'] = array('Email', 'Phone');

 

foreach($Menu as $Parent=>$v)

{

//Echo $Parent so that you have the parent of the list

foreach($v as $Child)

{

// Here you echo $Child so that you have the Children of said Parent

}

//End the list here

}

I suppose that rather than ask everyone to go elsewhere to see the code issues, I'll post it...

 

CSS

** Links
*/
a { text-decoration: none; color: #6e7549; }
a:hover { text-decoration: none; color: #ccc;}

#main-menu {
margin-left: 220px;
margin-right: 50px;
height: 93px;
}

#main-menu ul {
list-style-type: none;
margin: 0;
padding: 0;
text-align: right;
padding-top: 45px;
}
#main-menu li {
display: inline;
padding: 20px 10px 3px 10px;
border-right: 1px solid #CCC;
}
#main-menu li ul {
margin:0px;
padding:0px;
display:none;
position:absolute;
left:0px;
top:20px;
background-color:#000;
}
#main-menu li.last {
border-right: 0px none;
}
#main-menu li a {
color: #000;
/*text-transform: uppercase;*/
font-variant: small-caps;
}
#main-menu li ul a {
color: #000;
/*text-transform: uppercase;*/
font-variant: small-caps;
}
#main-menu li a:hover {
color: #6e7549;
}
#main-menu li ul a:hover {
color: #6e7549;
}

 

From the functions.php file

// Get a menu based on an array
//
function get_menu($menu = array(), $ulclass = '', $is_main_menu = false) {
global $menu_selected;
$output = '';
if (empty($menu)) {
return $output;
}

$output .= '<ul' . (!empty($ulclass) ? (' class="' . $ulclass . '"') : '') . '>';
foreach($menu as $item) {
if (!$is_main_menu || !isset($item['hide_in_main']) || !$item['hide_in_main']) {
$li_class = (isset($item['sub']) && !empty($item['href']) ? ('dir') : '');
if (isset($menu_selected) && !empty($menu_selected) && $menu_selected == $item['href']) {
$li_class = (!empty($li_class)) ? $li_class . ' selected' : 'selected';
}
if (isset($item['li_class']) && !empty($item['li_class'])) {
$li_class .= (!empty($li_class)) ? ' ' . $item['li_class'] : $item['li_class'];
}
$output .= '<li' . (!empty($li_class) ? ' class="' . $li_class . '"': '' ) . '>';
$output .= '<a';
if (isset($item['href']) && !empty($item['href'])) {
$output .= ' href="' . $item['href'] .'"';
}
if (isset($item['title']) && !empty($item['title'])) {
$output .= ' title="' . $item['title'] .'"';
}
if (isset($item['class']) && !empty($item['class'])) {
$output .= ' class="' . $item['class'] .'"';
}
if (isset($item['target']) && !empty($item['target'])) {
$output .= ' target="' . $item['target'] .'"';
}
$output .= '>';
if (isset($item['title']) && !empty($item['title'])) {
$output .= $item['title'];
} else if (isset($item['href']) && !empty($item['href'])) {
$output .= $item['href'];
}
$output .= '</a>';
if (isset($item['sub']) && !empty($item['sub'])) {
$output .= get_menu($item['sub'], $ulclass);
}
$output .= '</li>';
}
}
$output .= '</ul>';
return $output;
}

//

 

From the config.php file

// Site Menu
//
$menu[] = array(
'title' => 'Home',
'href' => 'index.php'
);
$menu[] = array(
'title' => 'Activities',
);
$menu[] = array(
'title' => 'Winter Activities',
'href' => 'wactivities.php'
);
$menu[] = array(
'title' => 'Image Gallery',
'href' => 'gallery.php'
);

 

Thanks In Advance

Simple.

 

$Menu['Valo Services'] = array('Valo Home Watch', 'Value Home Renovations', 'Valo Outdoor Living Spaces');

$Menu['Contact Valo'] = array('Email', 'Phone');

 

foreach($Menu as $Parent=>$v)

{

//Echo $Parent so that you have the parent of the list

foreach($v as $Child)

{

// Here you echo $Child so that you have the Children of said Parent

}

//End the list here

}

 

ok so i put this in the config file in place of the menu items as they are now i assume?

Simple.

 

$Menu['Valo Services'] = array('Valo Home Watch', 'Value Home Renovations', 'Valo Outdoor Living Spaces');

$Menu['Contact Valo'] = array('Email', 'Phone');

 

foreach($Menu as $Parent=>$v)

{

//Echo $Parent so that you have the parent of the list

foreach($v as $Child)

{

// Here you echo $Child so that you have the Children of said Parent

}

//End the list here

}

 

ok so i put this in the config file in place of the menu items as they are now i assume?

 

That's an example of how to do it. I'm sorry that I'm not doing your code for you, but I've grown bored lately, so I generally only post content to help people learn, not to do the work for the people

Simple.

foreach($Menu as $Parent=>$v)

{

//Echo $Parent so that you have the parent of the list

foreach($v as $Child)

{

// Here you echo $Child so that you have the Children of said Parent

}

//End the list here

}

 

I understand that this is the drive part, but i'm sorry i really dont get it..

i got here and really screwed it up.... completely broke page...

$Menu['Activities'] = array('Summer Activities', 'Winter Activities');
foreach($Menu as $Parent=>$v)
{
$Parent =>'Activities'
}
foreach($v as $Child)
{
$Child[] = array(
'title' => 'Summer Activities',
'href' => 'activities.php'
$Child[] = array(
'title' => 'Winter Activities',
'href' => 'wactivities.php'
}
//End the list here
}

What? Nononono.

 

//Echo $Parent so that you have the parent of the list

 

That is where you put

 

echo "<Some html that makes the text 'Valo Services' or 'Contact Valo' the parent of the list>$Parent</that html>";

 

// Here you echo $Child so that you have the Children of said Parent

 

Is here you put

 

echo "<The html that does the dropdown that displays the child>$Child</that html>";

 

//End the list here

 

Is where you put

 

echo "Whatever closes the dropdown";

I’m not trying to confuse you but if you wanted to make the code work for more than two levels you could create a recursively calling function that would display all of the levels in an array until there are no child items.

 

To help yourself learn before creating the PHP code to do the behind the scenes work you may try creating a pure html version of your page with only two or three menu items with a couple of child items each.

 

You will then have the code and be able to look at it more closely to see what you would need to echo with the PHP to create the same effect, you could then compare the source code of the pages that are statically crated and dynamically created to do some debugging.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.