Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Check reviews of PHP Web Hosting, compatible with Smarty Templates

Largest classified with thousands of ads at Shoppok

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy wedding dresses on SiteSteer.com - Best Online Wedding Dress Store

Candy Crush Cheats

Tired of playing games? Visit the Game Supply to Sell World of Warcraft Accounts.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at Bestpriceglasses.com. Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

{html_options}

{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
  • 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 8.12. Associative array with the options attribute


<?php
$smarty->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.


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

  

Output of the above example would be:


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


Example 8.13. Dropdown with seperate arrays for values and ouptut


<?php
$smarty->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 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}
</select>

  

The above example would output:


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


  

Example 8.14. Database example (eg ADODB or PEAR)


<?php

$sql = 'select type_id, types from contact_types order by type';
$smarty->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.


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

  

Example 8.15. Dropdown's with <optgroup>


<?php
$arr['Sport'] = array(6 => 'Golf', 9 => 'Cricket',7 => 'Swim');
$arr['Rest']  = array(3 => 'Sauna',1 => 'Massage');
$smarty->assign('lookups', $arr);
$smarty->assign('fav', 7);
?>

  

The script above and the following template


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

  

would output:


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


See also {html_checkboxes} and {html_radios}

Comments
by Gogowitsch on Apr 15, 2012 at 1:13
Example 8.13 will not render as expected on Firefox 11 (invalid HTML), as {html_options} will create another <select> within the outer.
by Lajos Pirk on Dec 18, 2011 at 8:52
Hello, Is that possible to make this function work with multiple select option? I mean, theoretically i can pass an array in the selected parameter, but if i do, the function makes nothing to selected in the result. :(
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 0 plus 11? (Are you human?)

Sponsors [info]

UK Web Hosting @webhost.uk.net
Best Web Hosting @rshosting.com
Web Hosting UK @webhostinguk.com
Unlimited Web Hosting @infrenion.com
App Entwicklung @morphodo.com
Buy from China @dhgate.com
dissertationteam.com
Buy Web Hosting @buy-hosting.net
WordPress Hosting Hub @wphostinghub.net