I want to have a dropdown list with
United Kingdom
Country 1
Country 2
Country 3
Country 4
Country 5
Country 6
Country 7
Country 8
United Kingdom
Country 9
Country 10
Country 11
How do I achieve this as United kingdom has the same value in the option (therefore I cant pass an array with the same key twice)
Thanks
Page 1 of 1
Duplicate options in dropDownList
#2
Posted 06 May 2011 - 09:46 AM
It's impossible with an ordinary 1 level array like this.
The 2nd one will just overwrite the 1st one, and no duplicate entries will be created.
You have to use another key for the 2nd entry while you may use the same label for it.
So, how about this?
Using a 2 level array, it will group the items.
The dropDownList will show something like:
category 1
category 2
$countries = array( 1 => 'country 1', 2 => 'United Kingdom', 3 => 'country 3', .... 7 => 'country 7', 2 => 'United Kingdom', 8 => 'country 8, .... ); echo CHtml::dorpDownList('country', $country_id, $countries);
The 2nd one will just overwrite the 1st one, and no duplicate entries will be created.
You have to use another key for the 2nd entry while you may use the same label for it.
So, how about this?
Using a 2 level array, it will group the items.
$countries = array( 'category 1' => array( 1 => 'country 1', 2 => 'United Kingdom', 3 => 'country 3', ), 'category 2' => array( 7 => 'country 7', 2 => 'United Kingdom', 8 => 'country 8, ), ); echo CHtml::dorpDownList('country', $country_id, $countries);
The dropDownList will show something like:
category 1
country 1
United Kingdom
country 2
United Kingdom
country 2
category 2
country 7
United Kingdom
country 8
United Kingdom
country 8

#3
Posted 06 May 2011 - 10:55 AM
And why would you want a duplicated entry in your list anyways?
- Extension: Dynamic DataProvider Behavior
- Extension: XUpload - jQuery File Upload Extension
- Extension: PhpQuickProfiler - A Web Log Router that will help you profile your application
- Extension: XDateView - A Date grouped Grid View
- Extension: Foundation - An easy to use, powerful, and flexible framework for building prototypes and production code on any kind of device.
- Wiki: How to generate Yii like Documentation
- Wiki: How to re enable logging during unit testing
- Extension: XUpload - jQuery File Upload Extension
- Extension: PhpQuickProfiler - A Web Log Router that will help you profile your application
- Extension: XDateView - A Date grouped Grid View
- Extension: Foundation - An easy to use, powerful, and flexible framework for building prototypes and production code on any kind of device.
- Wiki: How to generate Yii like Documentation
- Wiki: How to re enable logging during unit testing
#4
Posted 06 May 2011 - 11:40 AM
andyofb, on 06 May 2011 - 06:13 AM, said:
I want to have a dropdown list with
United Kingdom
Country 1
Country 2
Country 3
Country 4
Country 5
Country 6
Country 7
Country 8
United Kingdom
Country 9
Country 10
Country 11
How do I achieve this as United kingdom has the same value in the option (therefore I cant pass an array with the same key twice)
Thanks
United Kingdom
Country 1
Country 2
Country 3
Country 4
Country 5
Country 6
Country 7
Country 8
United Kingdom
Country 9
Country 10
Country 11
How do I achieve this as United kingdom has the same value in the option (therefore I cant pass an array with the same key twice)
Thanks
In CHtml::listOptions() or CHtml::dropDownList (I tried the latter), the htmlOption empty can do it.
array( 'empty'=>array('', 'someindex'=>'somelabel', 'otherindex'=>'otherlabel'), )
/Tommy
Share this topic:
Page 1 of 1