Hierarchical Dropdown List (not menu)

I need to implement a hierarchical dropdownlist for my application something that is just like the regular dropdownlist but can handle multiple levels would work. Something like what home depot’s site has on the frontpage “Shop All Departments” would be GREAT!!!

Option Parent 1

Option Child 1

Option Child 2

Option Parent 2

Option Child 1

Option Child 2

This might help you:

DB Structure




CREATE TABLE IF NOT EXISTS `cms_menu` (

  `menu_id` int(11) NOT NULL auto_increment,

  `name` varchar(255) NOT NULL,

  `date_added` datetime NOT NULL,

  `last_updated` datetime NOT NULL,

  `status` enum('active','inactive') NOT NULL,

  PRIMARY KEY  (`menu_id`),

  UNIQUE KEY `name_UNIQUE` (`name`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;


CREATE TABLE IF NOT EXISTS `cms_menu_item` (

  `item_id` int(11) NOT NULL auto_increment,

  `parent_id` int(11) default NULL,

  `menu_id` int(11) NOT NULL,

  `label` varchar(255) NOT NULL,

  `url` text NOT NULL,

  `description` text NOT NULL,

  `date_added` datetime NOT NULL,

  `last_updated` datetime NOT NULL,

  `sort_order` int(11) NOT NULL,

  `status` enum('active','inactive') NOT NULL,

  PRIMARY KEY  (`item_id`),

  KEY `fk_cms_menu_item_cms_menu1` (`menu_id`),

  KEY `fk_cms_menu_item_cms_menu_item1` (`parent_id`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;


ALTER TABLE `cms_menu_item`

  ADD CONSTRAINT `fk_cms_menu_item_cms_menu1` FOREIGN KEY (`menu_id`) REFERENCES `cms_menu` (`menu_id`) ON DELETE CASCADE ON UPDATE NO ACTION,

  ADD CONSTRAINT `fk_cms_menu_item_cms_menu_item1` FOREIGN KEY (`parent_id`) REFERENCES `cms_menu_item` (`item_id`) ON DELETE SET NULL ON UPDATE NO ACTION;



The code to extract the array that will be later processed by CMENU.




public function getItems($menu_id, $parent_id=null)

{

	$results = Yii::app()->getDb()->createCommand();

	$results->select('item_id, label, url')->from('{{menu_item}}');

	

	if($parent_id === null)

		$results->where('menu_id=:mid AND parent_id IS NULL', array(':mid'=>(int)$menu_id));

	else

		$results->where('menu_id=:mid AND parent_id=:pid', array(':mid'=>(int)$menu_id, ':pid'=>$parent_id));

		

	$results->order('sort_order ASC, label ASC');

	$results = $results->queryAll();

	

	$items = array();

	

	if(empty($results))

		return $items;

		

	foreach($results AS $result)

	{

	    $childItems=$this->getItems($menu_id, $result['item_id']); 

            $items[] = array(

	       'label'         => $result['label'],

               'url'           => $result['url'],

               'itemOptions'   => array(),// array('class'=>'listItem'),

               'linkOptions'   => array(),// array('class'=>'listItemLink', 'title'=>$result['label']),

               'submenuOptions'=> array(),

               'items'         => $childItems, 

	     );

	}

    

	return $items;

}



And show the menu ID 2 items using CMenu widget:




$items=$this->getItems(2);

$menu = array(

  'id' => 'nav',

  'activeCssClass'=>'selected',

  'linkLabelWrapper'=>null, 

  'htmlOptions'=>array('class'=>'topNav'),

  'items'=>$items

);

$this->widget('zii.widgets.CMenu', $menu);



Simple eh ?

:lol: great example , helpful for designing menu system .

i just realized that this could be a great wiki example of database driven CMENU, so i will add it there too :)

L.E:

Added to wiki: http://www.yiiframework.com/wiki/295/database-driven-cmenu/

:lol:

you see ! it 's a popular wiki now .well done :D

Thanks for the example, I appreciate it. It does help me with the Database part and helps me move to the next step. I will try it out in my application and I am sure I will be able to move further. Thanks once again. Look forward to learning more and contributing to the Yii community as well.

Awesome Sala

Hi @twisted1919

Can you give a full example or files that we can view in detail, that will be helpful,

best regards