Dropdown Value

I create a component file Country inside components folder. It have a function that returns country list from API. I am using this function for drop downlist in a form. The country listed correctly in drop list, but on saving time the corresponding id of country is saved in db. How it solve? I want to save the country name.

The drop down code

<?php echo $form->dropdownlist($model,'country',  Country::getCountry(),array('style'=>'width: 125px','empty'=>array('Select'=>'Select')));?>

try this

echo $form->dropdownlist($model,'country',  CHtml::listData(Country::model()->findAll(), 'value_field', 'text_field'));

in CHtml::listData , value_field is which you want to save to db and text_field is to display

in dropdown list

Better to add in table called country and generate model.

Hey, The country list not loaded from a model for trying like this. The component function returns only the country list as an array.

Add a model for simple country list from API ? I dont think thats a good idea.

public string dropDownList(CModel $model, string $attribute, array $data, array $htmlOptions=array ( ))

$data is array of (value=>display), so probably your function passing it like id=>country_name, You should change this getCountry() method to set array like (country_name=>country_name)

And i think that new model is not needed here at all.

what is the output of Country::getCountry() ?

change there the id with name .

function returns country list as array.

2d array?

pls list 2 or 3 lines of the output ?


 $new= Country::getCountry();


    echo "<br>";



It shows the output like this

Array ( [0] => Afghanistan [1] => Aland Islands [2] => Albania [3] => Algeria [4] => American Samoa

And like i said before You have to change your key (replace it with value).

is this tried ?

CHtml::listData(Country::getCountry()) ?

Yes. But not working. listData() need two parameters. Also tried this CHtml::listData(Country::getCountry(),’ ', ’ ') , but it doesn’t load the country list.

  $data= Country::getCountry();

    foreach($data $strn)






 echo $form->dropdownlist($model,'country',$list,array('style'=>'width: 125px','empty'=>array('Select'=>'Select')));

this will work. i think there no other simple method . as u said or any change in api will get the same array

It works fine… Thanks :)