andyofb
(Andy)
1
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
softark
(Softark)
2
It’s impossible with an ordinary 1 level array like this.
$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
[indent]country 1
United Kingdom
country 2[/indent]
category 2
[indent]country 7
United Kingdom
country 8[/indent]

Asgaroth
(Asgaroth Belem)
3
And why would you want a duplicated entry in your list anyways?
tri
(tri - Tommy Riboe)
4
In CHtml::listOptions() or CHtml::dropDownList (I tried the latter), the htmlOption empty can do it.
array(
'empty'=>array('', 'someindex'=>'somelabel', 'otherindex'=>'otherlabel'),
)
/Tommy