{html_options} is a custom function that creates html <select><option> group with provided data. It takes care of which item(s) are selected by default as well. Required attributes are values and output, unless you use options instead.

Attribute NameTypeRequiredDefaultDescription
valuesarrayYes, unless using options attributen/aan array of values for dropdown
outputarrayYes, unless using options attributen/aan array of output for dropdown
selectedstring/arrayNoemptythe selected option element(s)
optionsassociative arrayYes, unless using values and outputn/aan associative array of values and output
namestringNoemptyname of select group

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 output is XHTML compatible.

If the optional name attribute is given, the <select name="groupname"></select> tags will enclose the option list. Otherwise only the <option> list is generated.

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.

Example 8-10. {html_options}

Example 1:


->assign('cust_ids', array(1000,1001,1002,1003));
$smarty->assign('cust_names', array(
'Joe Schmoe',
'Jack Smith',
'Jane Johnson',
'Charlie Brown'));


Where template is:

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

Example 2:


->assign('cust_options', array(
1000 => 'Joe Schmoe',
1001 => 'Jack Smith',
1002 => 'Jane Johnson',
1003 => 'Charlie Brown')


where template is:

{html_options name=customer_id options=$cust_options selected=$customer_id}

Both the above examples will output:

<select name="customer_id">
    <option label="Joe Schmoe" value="1000">Joe Schmoe</option>
    <option label="Jack Smith" value="1001" selected="selected">Jack Smith</option>
    <option label="Jane Johnson" value="1002">Jane Johnson</option>
    <option label="Charlie Brown" value="1003">Charlie Brown</option>

Example 8-11. {html_options} - Database example (eg PEAR or ADODB):


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

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


where the template is:

<select name="type_id">
    <option value='null'>-- none --</option>
    {html_options name="type" options=$types selected=$contact.type_id}

See also {html_checkboxes} and {html_radios}

© Copyright 2003-2014 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.