Thanks Spaiz, that works for me
kodegeek 0 Newbie Poster
Perfect!
We can create multi level drop down using php/mysql .
we can also create up to unlimited level of drop down using this code.first i am creating a mysql table
CREATE TABLE IF NOT EXISTS `navi_links` ( `link_id` smallint(6) NOT NULL auto_increment, `link_parent` smallint(6) NOT NULL, `link_url` varchar(255) collate latin1_general_ci NOT NULL, `link_name` varchar(255) collate latin1_general_ci NOT NULL, `display_order` smallint(6) NOT NULL, PRIMARY KEY (`link_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=11 ;
now i am creating one recursive function which i will call number of times till the parent exist.
// prepare special array with parent-child relations $menuData = array( 'items' => array(), 'parents' => array() ); $result = mysql_query("SELECT link_id id, link_parent parentId, link_name name FROM navi_links ORDER BY link_parent"); while ($menuItem = mysql_fetch_assoc($result)) { $menuData['items'][$menuItem['id']] = $menuItem; $menuData['parents'][$menuItem['parentId']][] = $menuItem['id']; } // menu builder function, parentId 0 is the root function buildMenu($parentId, $menuData) { $html = ''; $parent=''; if (isset($menuData['parents'][$parentId])) { $menuClass= ($parentId==0) ? ' class="menu" id="menu"' : ''; $parent= ($parentId==0) ? 0 : 1; $html = "<ul{$menuClass}>\n"; foreach ($menuData['parents'][$parentId] as $itemId) { //subment $result=mysql_query("select * from navi_links where link_parent='$itemId'"); if (mysql_num_rows($result)>(int)0 && $parentId!=0) { $subm =' class="sub"'; }else{ $subm =''; } //end $menu = $parentId == 0 ? ' class="menulink"' : ''; //class of main menu $html .= '<li>' . "<a{$subm}{$menu} href=\"#\" title=\"{$row['title']}\">{$menuData['items'][$itemId]['name']}</a>"; // find childitems recursively $html .= buildMenu($itemId, $menuData); $html .= '</li>'; } $html .= '</ul>'; } return $html; } // output the menu echo buildMenu(0, $menuData);