Beste forum leden,

Ik krijg het maar niet voor elkaar om mijn menu items actief te maken. Alles werkt zoals ik het hebben wil zolang er geen items onder parents hangen.

Maar ik wil ook dat de parent active wordt als er een item die daaronder hangt aangeklikt wordt.

Dit is de code, ik hoop dat het een beetje duidelijk is:

<?php  function getMenu()
       {

       		// Select all entries from the menu table
			$result = mysql_query("SELECT * FROM menu_items WHERE status = 1 ORDER BY parent, sort, label");
            $num_rows = mysql_num_rows($result);          
            
			// Create a multidimensional array to contain a list of items and parents
			$menu = array
            (
			    'items' => array(),
			    'parents' => array()
			);
            
			// Builds the array lists with data from the menu table
			$menu_name = '';
            while ($items = mysql_fetch_assoc($result))
			{
			    // Creates entry into items array with current menu item id ie. $menu['items'][1]
			    $menu['items'][$items['id']] = $items;
			    // Creates entry into parents array. Parents array contains a list of all items with children
			    $menu['parents'][$items['parent']][] = $items['id'];
                
                $menu_name = 'menu_'.$items['id'];
  
			}
   
            $q = 'SELECT status FROM layouts WHERE position_id = "'.getPositionIdByPositionName('menu').'" AND page_id = "'.getPageIdByPage().'" '; 
            $result = mysql_query($q) or die(mysql_error());

            $menu_status_id = 0;
            while ($menu_status = mysql_fetch_assoc($result))
            {
            
            	$menu_status_id = $menu_status['status'];

            }   

                      if ( $menu_status_id == 1 )
                      {
                          echo  "<div id='menu'>";
                          echo buildMenu(0, $menu);
                          echo "</div>";
                      }	

       }
 
     		// Menu builder function, parentId 0 is the root
            function buildMenu($parent, $menu)
            {

               $html = "";
               if (isset($menu['parents'][$parent]))
               {
						
                  $html .= "
                  
                  <ul>\n";
                   foreach ($menu['parents'][$parent] as $itemId)
                   {
                      if(!isset($menu['parents'][$itemId]))
                      {
                         
                         if (getCurrentPage() == $menu['items'][$itemId]['link'])
                         {
                         	$active = "class='active'";
                         	
                         }
                         else
                         {
                         	$active = '';
                         	
                         }
                         
                         $html .= "<li ".$active.">\n  <a ".$active." href='".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";
                      }
                      if(isset($menu['parents'][$itemId]))
                      {
                         $html .= "
                         <li ".$active.">\n  <a ".$active." href='".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a> \n";
                         $html .= buildMenu($itemId, $menu);
                         $html .= "</li> \n";
                      }
                   }
                   $html .= "</ul> \n";
               }
               return $html;
            } ?>
Ik heb de code van Kris overgenomen en het geprobeerd zo te krijgen zoals ik het wil maar het lukt nog niet helemaal.

Alle items worden wel geshowed maar ze komen niet onder hun parent te hangen...
Ik doe waarschijnlijk iets fout tijdens het opbouwen van de html.

Dit is mijn huidige code nu, ik hoop dat er iemand is die mij kan helpen om de items onder hun parent te krijgen, alvast super bedankt!

<?php        	/**
		* recursieve functie.  zoekt het item met gegeven id.  Geeft je een breadcrumb (omgekeerde volgorde).
		* Alle items in de breadcrumb krijgen een ['active']=1
		* $items wordt by ref doorgegeven, zo kunnen we de gegevens aanpassen.
		*/
		function searchInItems(&$items, $id, $breadcrumb = array()) 
		{
		  foreach ($items as $key=>$item) 
		  {
		    if($item['id'] == $id) 
		    {
		      $items[$key]['active'] = 1;
		      $breadcrumb[] = $item;
		      $a = searchInItems($items, $item['parent'], $breadcrumb);
		      if ($a) 
		      {
		        return $a;
		      }
		      break;
		    }
		  }
		  return $breadcrumb;
		}
       	
       	function buildMenu()
       	{
		// okay, stel, we zoeken de jonagold.  id=8
		// dan verwachten we dat "jonagold > appel > fruit" actief worden.
		$get_id = 8;
		
		$mysqli = new mysqli('localhost', '', '', '');  // zelf aanpassen
		$sql = "SELECT id, parent, sort, label, link, status
		  FROM menu_items
		  ORDER BY parent, sort";
		
		///////////////////////////////
		// Items halen en in array steken
		$items = array();
		$res = $mysqli->query($sql);
		while($row = $res->fetch_assoc()) 
		{
		  $items[] = $row;
		}
		
		///////////////////////////////
		// actief maken.  Je krijgt gratis een breadcrumb. ...
		$breadcrumb = searchInItems($items, $get_id);
		
		//echo '<hr>Breadcrumb: <pre>' . print_r($breadcrumb, 1) . '</pre>';
		
		//echo '<hr>Items: <pre>' . print_r($items, 1) . '</pre>';

		echo '<div id="menu"><ul>';
		
		foreach ($items as $menuItem)
		{
			
		if(!empty($menuItem['active'])) 
		{
			$active = 'class="active"';
		}
		else
		{
			$active = '';
		}
			
			
			echo '<li '.$active.'><a '.$active.' href='.$menuItem['link'].'>'.$menuItem['label'].'</a></li>' ;
		}
		
		//echo '<li><a href="'.$items['link'].'">'. print_r($items, 1) . '</a></li>';
		
		
		
		echo '</ul></div>';
		
		
	} ?>


Reageren