Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store


{html_options} is a custom function that creates the html <select><option> group with the assigned data. It takes care of which item(s) are selected by default as well.

Attribute Name Type Required Default Description
values array Yes, unless using options attribute n/a An array of values for dropdown
output array Yes, unless using options attribute n/a An array of output for dropdown
selected string/array No empty The selected option element(s)
options associative array Yes, unless using values and output n/a An associative array of values and output
name string No empty Name of select group
strict boolean No FALSE Will make the "extra" attributes disabled and readonly only be set, if they were supplied with either boolean TRUE or string "disabled" and "readonly" respectively
  • 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> list is 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.

Example 141. Associative array with the options attribute

->assign('myOptions', array(
1800 => 'Joe Schmoe',
9904 => 'Jack Smith',
2003 => 'Charlie Brown')


The following template will generate a drop-down list. Note the presence of the name attribute which creates the <select> tags.

{html_options name=foo options=$myOptions selected=$mySelect}


Output of the above example would be:

<select name="foo">
<option value="1800">Joe Schmoe</option>
<option value="9904" selected="selected">Jack Smith</option>
<option value="2003">Charlie Brown</option>

Example 142. Dropdown with separate arrays for values and ouptut

->assign('cust_ids', array(56,92,13));
$smarty->assign('cust_names', array(
'Joe Schmoe',
'Jane Johnson',
'Charlie Brown'));


The above arrays would be output with the following template (note the use of the php count() function as a modifier to set the select size).

<select name="customer_id" size="{$cust_names|@count}">
   {html_options values=$cust_ids output=$cust_names selected=$customer_id}


The above example would output:

<select name="customer_id" size="3">
    <option value="56">Joe Schmoe</option>
    <option value="92" selected="selected">Jane Johnson</option>
    <option value="13">Charlie Brown</option>


Example 143. Database example (eg ADODB or PEAR)


'select type_id, types from contact_types order by type';

$sql 'select contact_id, name, email, contact_type_id
        from contacts where contact_id='


Where a template could be as follows. Note the use of the truncate modifier.

<select name="type_id">
    <option value='null'>-- none --</option>
    {html_options options=$contact_types|truncate:20 selected=$contact.type_id}


Example 144. Dropdown's with <optgroup>

['Sport'] = array(=> 'Golf'=> 'Cricket',=> 'Swim');
$arr['Rest']  = array(=> 'Sauna',=> 'Massage');


The script above and the following template

{html_options name=foo options=$lookups selected=$fav}


would output:

<select name="foo">
<optgroup label="Sport">
<option value="6">Golf</option>
<option value="9">Cricket</option>
<option value="7" selected="selected">Swim</option>
<optgroup label="Rest">
<option value="3">Sauna</option>
<option value="1">Massage</option>

See also {html_checkboxes} and {html_radios}

Smarty Template Engine