[eluser]Dr. Seuss[/eluser]
For anyone who is interested, here is the solution:
Code:
$query = $this->db->query('SELECT distinct city, state FROM loc ORDER BY state, city');
$options = array();
$options['null']=$emptyString; // first item in list is 'empty'
$prevState = "";
foreach ($query->result_array() as $row){
if($prevState == $row['state']){
${$prevState}[$row['state'].'_'.$row['city']]=' '.$row['city'].' ';
}else{
if($prevState!=""){
$options[$prevState]=$$prevState;
};
$prevState = $row['state'];
$$prevState = array();
${$prevState}[$row['state'].'_'.$row['city']]=' '.$row['city'].' ';
}
}
form_dropdown('city', $options,$emptyString);
The solution requires creating a dynamic variable ('$$prevState') of type array, populating that array, finally adding the dynamically created array to the '$options' array. Hopefully this will help someone else.
Oh, and for anyone who needs a static example of the structure for making an optgroup, you can see it here
http://ellislab.com/forums/viewthread/49348/
////////////////////////
ORIGINAL POST BELOW
////////////////////////
Per the form_dropdown on this
page.
Quote:If the array passed as $options is a multidimensional array, form_dropdown() will produce an <optgroup> with the array key as the label.
Does anyone have a sample array that yields correct optgroups c lusters?
I am wanting to create a dropdown list of cities grouped by state, using the query:
Quote:$query = $this->db->query('SELECT distinct city, state FROM az ORDER BY state, city');
The way that I would interpret the correct way to make the multidimensional array would be:
Quote:foreach ($query->result_array() as $row)
{
$options[$row['state']] = array($row['city'], ' '.$row['city'].' ');
}
But this yields the following HTML:
Quote:<select name="city">
<optgroup label="AL ">
<option value="0" selected="selected">TALLADEGA </option>
<option value="1">TALLADEGA </option>
</optgroup>
<optgroup label="AR ">
<option value="0" selected="selected">MAGNOLIA </option>
<option value="1">MAGNOLIA </option>
</optgroup>
<optgroup label="FL ">
<option value="0" selected="selected">ZEPHYRHILLS </option>
<option value="1">ZEPHYRHILLS </option>
</optgroup>
<optgroup label="GA ">
<option value="0" selected="selected">WOODSTOCK </option>
<option value="1">WOODSTOCK </option>
</optgroup>
<optgroup label="KS ">
<option value="0" selected="selected">WICHITA </option>
<option value="1">WICHITA </option>
</optgroup>
<optgroup label="LA ">
<option value="0" selected="selected">WINNFIELD </option>
<option value="1">WINNFIELD </option>
</optgroup>
<optgroup label="NC ">
<option value="0" selected="selected">WINTERVILLE </option>
<option value="1">WINTERVILLE </option>
</optgroup>
<optgroup label="NM ">
<option value="0" selected="selected">PORTALES </option>
<option value="1">PORTALES </option>
</optgroup>
<optgroup label="OK ">
<option value="0" selected="selected">WOODWARD </option>
<option value="1">WOODWARD </option>
</optgroup>
<optgroup label="SC ">
<option value="0" selected="selected">YORK </option>
<option value="1">YORK </option>
</optgroup>
<optgroup label="TN ">
<option value="0" selected="selected">MOUNTAIN CITY </option>
<option value="1">MOUNTAIN CITY </option>
</optgroup>
<optgroup label="VA ">
<option value="0" selected="selected">WYTHEVILLE </option>
<option value="1">WYTHEVILLE </option>
</optgroup>
<optgroup label="WV ">
<option value="0" selected="selected">PRINCETON </option>
<option value="1">PRINCETON </option>
</optgroup>
</select>
I feel like I am doing something really stupid. Any ideas?