About dropDownList selected value

How to use dropDownList ?

if i have array data vehicle

"0" = "bicycle"

"1" = "motorcycle"

"2" = "car"

"3" = "bus" <selected>

"4" = "truck"

I choosse bus as selected data, is there any tutorial ? or exampe source code ?




$items = [


"0" => "bicycle"

"1" => "motorcycle"

"2" => "car"

"3" => "bus" <selected>

"4" => "truck"


];


echo yii\helpers\Html::dropDownList('name_of_select_field', 'value_of_select_field', $items);



Is it enough?

if i using array

$vehicle = ["0" => "bicycle", "1" => "motorcycle", "2" => "car" , "3" => "bus", "4" => "truck"];

echo yii\helpers\Html::dropDownList(‘3’, ‘bus’, $vehicle);

its true??

You have swapped parameters. First is select tag name and second is selected value.

It should be:




 echo yii\helpers\Html::dropDownList('bus', '3', $vehicle); 



its same if i m using

yii\widgets\ActiveForm::dropDownList()

The only difference is that




 yii\widgets\ActiveForm::dropDownList()



is the same of Html::activeDropDownList (notice active prefix).

and requires a $model instance as first parameter.

Html::dropDownList, instead, creates a dropdown list using only select tag name.