Yii Framework Forum: Dropdown Value - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Dropdown Value Rate Topic: -----

#1 User is offline   Yii123 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 13-March 13

Posted 27 March 2013 - 02:45 AM

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')));?>

0

#2 User is offline   Lal Zada 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 06-November 12
  • Location:Lahore, Pakistan

Posted 27 March 2013 - 03:04 AM

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
Is it fast? Is it secure? Is it professional? Is it right for my next project? Yes, it is

Linkedin Facebook Twitter Google+
0

#3 User is offline   Balu 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 153
  • Joined: 09-August 11
  • Location:Kerala, India

Posted 27 March 2013 - 04:59 AM

Better to add in table called country and generate model.
0

#4 User is offline   Yii123 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 13-March 13

Posted 27 March 2013 - 05:02 AM

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

#5 User is offline   mirunho 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 19-December 12
  • Location:Gdansk, Poland

Posted 27 March 2013 - 05:05 AM

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.
0

#6 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 911
  • Joined: 20-April 11
  • Location:India

Posted 27 March 2013 - 06:47 AM

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

change there the id with name .
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#7 User is offline   Yii123 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 13-March 13

Posted 27 March 2013 - 06:51 AM

function returns country list as array.
0

#8 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 911
  • Joined: 20-April 11
  • Location:India

Posted 27 March 2013 - 06:55 AM

View PostYii123, on 27 March 2013 - 06:51 AM, said:

function returns country list as array.


2d array?

pls list 2 or 3 lines of the output ?
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#9 User is offline   Yii123 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 13-March 13

Posted 27 March 2013 - 07:26 AM

<?php
 $new= Country::getCountry();
    print_r($new);
    echo "<br>";
    
    ?>



It shows the output like this
Array ( [0] => Afghanistan [1] => Aland Islands [2] => Albania [3] => Algeria [4] => American Samoa
0

#10 User is offline   mirunho 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 19-December 12
  • Location:Gdansk, Poland

Posted 27 March 2013 - 07:34 AM

View PostYii123, on 27 March 2013 - 07:26 AM, said:

<?php
 $new= Country::getCountry();
    print_r($new);
    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).
0

#11 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 911
  • Joined: 20-April 11
  • Location:India

Posted 27 March 2013 - 07:37 AM

is this tried ?

CHtml::listData(Country::getCountry()) ?
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#12 User is offline   Yii123 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 13-March 13

Posted 27 March 2013 - 07:41 AM

View PostRajith R, on 27 March 2013 - 07:37 AM, said:

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.
0

#13 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 911
  • Joined: 20-April 11
  • Location:India

Posted 27 March 2013 - 07:42 AM


  $data= Country::getCountry();
    foreach($data $strn)
    {
        
        $list[$strn]=$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
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
1

#14 User is offline   Yii123 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 13-March 13

Posted 27 March 2013 - 07:51 AM

View PostRajith R, on 27 March 2013 - 07:42 AM, said:


  $data= Country::getCountry();
    foreach($data $strn)
    {
        
        $list[$strn]=$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 :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users