The form_dropdown() function doesn't have anyway to add attributes to the option elements. $extra is added to the select element.
If you pass a two-dimensional array to $options, it creates an optgroup element with the interior key/value pairs used for option elements inside the optgroup.
The best you could probably do if you still want to use form_dropdown() would be to create a new version of the function in MY_form_helper.php which takes an additional parameter with an array for the option attributes. In that case, I would recommend setting it up to require the same keys used in the $options array with the value being an array or string that can be passed to _attributes_to_string() (like the $extra parameter).
Here is an example. Most of the code here is just copied from the form_helper, and, other than the added parameter on the first line, I've marked the changed lines with comments indicating the changes. Note that although it supports adding attributes to option elements within optgroup elements, it doesn't support adding attributes to the optgroup elements themselves.
PHP Code:
function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '', $optionsExtra = array())
{
$defaults = array();
if (is_array($data))
{
if (isset($data['selected']))
{
$selected = $data['selected'];
unset($data['selected']); // select tags don't have a selected attribute
}
if (isset($data['options']))
{
$options = $data['options'];
unset($data['options']); // select tags don't use an options attribute
}
}
else
{
$defaults = array('name' => $data);
}
is_array($selected) OR $selected = array($selected);
is_array($options) OR $options = array($options);
// If no selected state was submitted we will attempt to set it automatically
if (empty($selected))
{
if (is_array($data))
{
if (isset($data['name'], $_POST[$data['name']]))
{
$selected = array($_POST[$data['name']]);
}
}
elseif (isset($_POST[$data]))
{
$selected = array($_POST[$data]);
}
}
$extra = _attributes_to_string($extra);
$multiple = (count($selected) > 1 && stripos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
$form = '<select '.rtrim(_parse_form_attributes($data, $defaults)).$extra.$multiple.">\n";
foreach ($options as $key => $val)
{
$key = (string) $key;
if (is_array($val))
{
if (empty($val))
{
continue;
}
$form .= '<optgroup label="'.$key."\">\n";
foreach ($val as $optgroup_key => $optgroup_val)
{
$sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
// Changed to include $optionsExtra
$form .= '<option value="'.html_escape($optgroup_key).'"'.$sel
.(isset($optionsExtra[$key][$optgroup_key]) ? _parse_form_attributes($optionsExtra[$key][$optgroup_key]) : '')
.'>'.(string) $optgroup_val."</option>\n";
}
$form .= "</optgroup>\n";
}
else
{
// Changed to include $optionsExtra
$form .= '<option value="'.html_escape($key).'"'
.(in_array($key, $selected) ? ' selected="selected"' : '')
.(isset($optionsExtra[$key]) ? _parse_form_attributes($optionsExtra[$key]) : '')
.'>'.(string) $val."</option>\n";
}
}
return $form."</select>\n";
}