3

I have table types and i want to build selectbox with all values from this table In my controller i wrote this code

$allRegistrationTypes = RegistrationType::model()->findAll();
$this->render('index', array('allRegistrationTypes' => $allRegistrationTypes))

How build selectbox in view file ?

yAnTar
  • 4,269
  • 9
  • 47
  • 73

3 Answers3

6

Well then its pretty simple all you need to do is first create List Data like

CHtml::ListData(allRegistrationTypes,'value you want to pass when item is selected','value you have to display');

for ex

typeList = CHtml::ListData(allRegistrationTypes,'id','type');

now remember both id and type are fields in table

now all you have to do is if you are using form then

<?php echo $form->dropDownList($model, 'type_id', $typeList, array('empty'=>'Select a tyoe')); ?>

and if you need multiple you can pass multiple => multiple in the array as htmlOptions

Afnan Bashir
  • 7,319
  • 20
  • 76
  • 138
2

You would use CHtml::dropDownList, or activeDropDownList if there is a "parent" model and you want to leverage its validation rules.

If you want to make the <select> element multiple-selection-capable, pass in 'multiple' => 'multiple' and 'size' => X as part of the $htmlOptions parameter.

Jon
  • 428,835
  • 81
  • 738
  • 806
1

Simplest Method to get "Select Box" in YII Framework:

<div class="row">
    <?php
        echo $form->labelEx($model,'county');
        $data = CHtml::listData(County::model()->findAll(), 'id', 'county');
        $htmlOptions =     array('size' => '1', 'prompt'=>'-- select county --', );
        echo $form->listBox($model,'county', $data, $htmlOptions);
        echo $form->error($model,'county');
    ?>
</div>

Good Luck..

nstCactus
  • 5,141
  • 2
  • 30
  • 41