Drop Down Menu with Models

How can I make the drop down list automatically gather all my models. So for example if I click on one of my Menu items "Products" I want all the products model to show on the drop down. I have seen a few extensions that supports drop down menus but nothing about gathering the list from models table.

I also need to be able to use images for the main menu items.

I’m slightly reluctant to start this because I always find a wiki or extension for this type of thing half way through. Can anyone direct me to an extension or possibly give me good guidelines?

Thanks

I think you have two ways to do it, preload and show/hide models, or ajax calls. I saw some tutorial about dependent dropdowns, it may help you.

There is this following extension that provides drop down menus but how I would I load the $models? how do I implement models into the sub menus?


<?php $this->widget('application.extensions.mbmenu.MbMenu',array( 

            'items'=>array( 

                array('label'=>'Home', 'url'=>array('/site/index')), 

                array('label'=>'Contact', 'url'=>array('/site/contact'), 

                  'items'=>array( 

                    array('label'=>'sub 1 contact'), 

                    array('label'=>'sub 2 contact'), 

                  ), 

                ), 

                array('label'=>'Test', 

                  'items'=>array( 

                    array('label'=>'Sub 1', 'url'=>array('/site/page','view'=>'sub1')), 

                    array('label'=>'Sub 2', 

                      'items'=>array( 

                        array('label'=>'Sub sub 1', 'url'=>array('/site/page','view'=>'subsub1')), 

                        array('label'=>'Sub sub 2', 'url'=>array('/site/page','view'=>'subsub2')), 

                      ), 

                    ), 

                  ), 

                ), 

            ), 

    )); ?> 

Thanks

Hi. Have you tried to loop through your model values and build the relevant submenu ‘items’ array?

Hi

So I can loop through my Models like so


for ($i=0; $i<=100; $i++){




 $cat= ECCategory::model()->findByPk($i);

echo ($cat==null) ? null : CHtml::link(CHtml::encode( $cat->Name), array('ECCategory/view', 'id'=>$cat->ID)).'<br/>'; 

}

That’s as far as I can go, not sure how to build the relevant submenu ‘items’ array. How do I place that code inside my submenus? I tried something like this but that obviously wasn’t successful


 'items'=>array( 

 array('label'=>'Sub 1', 'url'=>array('/site/page','view'=>'sub1')), 

    array(

    for ($i=0; $i<=100; $i++)

$cat= ECCategory::model()->findByPk($i);

echo ($cat==null) ? null : CHtml::link(CHtml::encode( $cat->Name), array('ECCategory/view', 'id'=>$cat->ID)),


                       

  ), 

  ), 

Cheers

Well I was thinking more like this:


$items = array();


foreach ($models as $model)

    $items[] = array('label' => $model->someAttribute, 'url' => …);


$this->widget('application.extensions.mbmenu.MbMenu',array( 

            'items'=>array( 

                array('label'=>'Home', 'url'=>array('/site/index')), 

                array('label'=>'Contact', 'url'=>array('/site/contact'), 

                  'items'=>array( 

                    array('label'=>'sub 1 contact'), 

                    array('label'=>'sub 2 contact'), 

                  ), 

                ), 

                array('label'=>'Test', 

                  'items'=>array( 

                    array('label'=>'Sub 1', 'url'=>array('/site/page','view'=>'sub1')), 

                    array('label'=>'Automatic generation from Model', 

                      'items' => $items, 

                    ), 

                  ), 

                ), 

            ), 

    )); ?>

Thanks Again Ben, your code was almost perfect just added 2-3 words to go past non-objects in my model. Also I put a limit too so I opted for the old skool for loop




$items = array();

	

	for ($i=0; $i<=100; $i++){

$model = ECCategory::model()->findByPk($i);

//foreach ($models as $model)


   ($model==null) ? null : $items[] = array('label' => $model->Name, 'url' => array('view', 'id'=>$model->ID)); 

	}

Appreciate your help

Hi guys I have a coding like this, how to create in mbmenu extension

$menuArray = array(array(‘label’ => ‘Home’, ‘url’ => array(’/site/index’)));

            if(Yii::app()-&gt;user-&gt;checkAccess('menuKecamatan')){


                array_push(&#036;menuArray,array('label' =&gt; 'Kecamatan', 'url' =&gt; array('/kecamatan/kecamatan/admin')));


            }


            if(Yii::app()-&gt;user-&gt;checkAccess('menuDesa')){


                array_push(&#036;menuArray,array('label' =&gt; 'Desa', 'url' =&gt; array('/desa/desa/admin')));


            }


            if(Yii::app()-&gt;user-&gt;checkAccess('menuBidang')){


                array_push(&#036;menuArray,array('label' =&gt; 'Bidang', 'url' =&gt; array('/bidang/bidang/admin')));


            }


            if(Yii::app()-&gt;user-&gt;checkAccess('menuKategori')){


                array_push(&#036;menuArray,array('label' =&gt; 'Kategori', 'url' =&gt; array('/kategori/kategori/admin')));


            }


            if(Yii::app()-&gt;user-&gt;checkAccess('menuJenis')){


                array_push(&#036;menuArray,array('label' =&gt; 'Jenis', 'url' =&gt;array('/jenis/jenis/admin')));


            }


            if(Yii::app()-&gt;user-&gt;checkAccess('menuKolom')){


                array_push(&#036;menuArray,array('label' =&gt; 'Table', 'url' =&gt; array('/table/table/admin')));


            }


            if(Yii::app()-&gt;user-&gt;checkAccess('menuUserManager')){


                array_push(&#036;menuArray,array('label' =&gt; 'User Manager', 'url' =&gt; Yii::app()-&gt;urlManager-&gt;createUrl('/userManager/user/admin')));


            }


            


            if(Yii::app()-&gt;user-&gt;checkAccess('menuAksesManajer')){


                array_push(&#036;menuArray,array('label' =&gt; 'Akses Manajer', 'url' =&gt; Yii::app()-&gt;urlManager-&gt;createUrl('/aksesManajer/aksesManajer/aksesManajer')));


            }

$menu = $menuArray;

$this->widget(‘application.extensions.mbmenu.MbMenu’, array(

                'items' =&gt; &#036;menu2));

@jagur2012

So what’s the issue you are facing with this code?

PS Where have you defined $menu2?