{html_options}{html_options} is a
custom function
that creates a html <select><option> group
with the assigned data. It takes care of which item(s) are selected by
default as well.
Attribute NameTypeRequiredDefaultDescriptionvaluesarrayYes, unless using options attributen/aan array of values for dropdownoutputarrayYes, unless using options attributen/aan array of output for dropdownselectedstring/arrayNoemptythe selected option element(s)optionsassociative arrayYes, unless using values and outputn/aan associative array of values and outputnamestringNoemptyname of select group
Required attributes are
values and output,
unless you use the combined options instead.
If the optional name attribute is given, the
<select></select> tags are created,
otherwise ONLY the <option>'s list are generated.
If a given value is an array, it will treat it as an html <optgroup>,
and display the groups. Recursion is supported with <optgroup>.
All parameters that are not in the list above are printed as
name/value-pairs inside the <select> tag. They are ignored if
the optional name is not given.
All output is XHTML compliant.
Associative array with the options attribute
assign('myOptions', array(
1800 => 'Joe Schmoe',
9904 => 'Jack Smith',
2003 => 'Charlie Brown')
);
$smarty->assign('mySelect', 9904);
?>
]]>
The following template will generate a drop-down list.
Note the presence of the name attribute
which creates the <select> tags.
Output of the above example would be:
]]>
Dropdown with seperate arrays for ouptut.
assign('cust_ids', array(56,92,13));
$smarty->assign('cust_names', array(
'Joe Schmoe',
'Jane Johnson',
'Charlie Brown'));
$smarty->assign('customer_id', 92);
?>
]]>
The above arrays would be output with the following template.
Note the sneaky use of the php count() function as a modifier
to set the select size.
{html_options values=$cust_ids output=$cust_names selected=$customer_id}
]]>
The above example would output:
]]>
Database example (eg ADODB or PEAR)
assign('contact_types',$db->getAssoc($sql));
$sql = 'select contact_id, name, email, contact_type_id
from contacts where contact_id='.$contact_id;
$smarty->assign('contact',$db->getRow($sql));
?>
]]>
Where a template could be as follows. Note the use of the truncate modifier.
{html_options options=$contact_types|truncate:20 selected=$contact.type_id}
]]>
Dropdown's with <optgroup>
'Golf', 9 => 'Cricket',7 => 'Swim');
$arr['Rest'] = array(3 => 'Sauna',1 => 'Massage');
$smarty->assign('lookups', $arr);
$smarty->assign('fav', 7);
?>
]]>
With the template would be
Output of the above example would be:
]]>
See also
{html_checkboxes}
and
{html_radios}