PHP Horizontal Menu with Sub Menu

Discussion in 'PHP' started by Ducimus, Nov 10, 2009.

  1. #1
    I just took over working on a site that has been coded in php.
    the menu is using an array
    // 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;
    }
    PHP:
    with the array inputs looking as:
    $menu[] = array(
    	'title' => 'Activities',
    	'href' => 'activities.php'
    );
    PHP:
    I would like to make this a drop down
    Parent: Activities
    Child 1: Summer
    Child 2: Winter

    is this doable? and I apologize in advance for posting so much code, i've disected most of the rest of it, just not this part.
     
    Ducimus, Nov 10, 2009 IP
  2. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you want a hierarchy

    Parent 1
    - Child 1
    - Child 2
    --Grandchild 1

    Or an actual drop-down box?
     
    Altari, Nov 10, 2009 IP
  3. organicCyborg

    organicCyborg Peon

    Messages:
    330
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Not sure why it's not working. But one thing caught me eye.
    
    if (isset($item['li_class']) && !empty($item['li_class'])) {
       $li_class .= (!empty($li_class)) ? ' ' . $item['li_class'] : $item['li_class'];
    }
    
    Code (markup):
    Checking if a variable is set, and not empty is the same thing. Just go with one or the other.
     
    organicCyborg, Nov 10, 2009 IP
  4. Ducimus

    Ducimus Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I suppose hierarchy... what i'm after is something like you see at subject2homeinspections.com (which i know is a joomla site cuz i'm the developer for it)
    I'm not really a coder so i'm a bit lost, I did not do the site originally, and will most likely have to get some of the coding validated.
    any help is greatly appreciated.
     
    Ducimus, Nov 11, 2009 IP
  5. Ducimus

    Ducimus Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    oh, organicCyborg the menu works as it is, i just want to expand it and make it more functional if possible.
     
    Ducimus, Nov 11, 2009 IP
  6. Ducimus

    Ducimus Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I posted this in another forum and got this response:

    $menu[] = array(
        'title' => 'Activities'
        ,'href' => 'activities.php'
        ,'children' => array(
            0 => array(
                    'title' => 'Subitem 1'
                    ,'href' => 'sub1.php'
            )
            ,1 => array(
                        'title' => 'Subitem 2'
                        ,'href' => 'sub2.php'
            )
        )
    );
    
    // then in your menu function
    if(array_key_exists('children', $item) && !empty($item['children']))
        $output .= get_menu($item['children']);  
    [PHP]
    
    though the person who posted it wasn't quite sure if it would work.
    PHP:
     
    Ducimus, Nov 11, 2009 IP