Last reviewed: Not reviewed


Builds an HTML <select> form field with <options> built from an array of values.  Both single item selects and multiple item selects are supported.


string selectList ( array &$options, string $tag_name, string $tag_attribs,
                                               string $key, string $text [, mixed $selected ] )
is an array of objects that have been returned by a query or the mosHTML::makeOption function.
is the name attribute of the HTML <select> tag.
is a string containing any additional attributes that you want to assign to the HTML <select> tag.
is the name of the class variable holding the option 'value'.  Should generally be 'value'.
is the name of the class variable holding the option 'text'.  Should generally be 'text'.
is either a string value for a single value select list or an array for a multiple value select list.  This parameter is optional and if omitted defaults to null.


Example 1: a single value select list:

// The option tag with the value of zero is selected.
$colours = array();
$colours[] = mosHTML::makeOption( '0', 'Red');
$colours[] = mosHTML::makeOption( '1', 'Green');
$colours[] = mosHTML::makeOption( '2', 'Blue');
$html = mosHTML::selectList( $colours, 'colour', 'size="1" class="inputbox"', 'value', 'text', 0 );
echo $html;

which produces:

<select name="colour" size="1" class="inputbox">
  <option value="0" selected="selected">Red</option>
  <option value="1">Green</option>
  <option value="2">Blue</option>

which renders as:

Example 2: a multiple value select list:

// alias the 'value' and 'text' fields and the array will
// be in the correct format.
$users = array();
$users[] = mosHTML::makeOption( '0', 'No user' );
$database->setQuery( "SELECT id AS value, username AS text FROM #__users" );
$users = array_merge( $users, $database->loadObjectList() );
// Get selected users from a ficticious table.
// We only need the 'value' to lookup the selected options.
$database->setQuery( "SELECT id AS value FROM #__users_selected" );
$selected = $database->loadObjectList();
// Creates the HTML.
// Note that 'multiple="true"' is required to make the HTML work properly.
$html = mosHTML::selectList( $users, 'user_ids',
       'size="10" class="inputbox" multiple="true"', 'value', 'text', $selected );
echo $html;

which might produce:

<select name="user_ids" size="10" class="inputbox" multiple="true">
  <option value="0">No user</option>
  <option value="1">Bill Williams</option>
  <option value="2" selected="selected">Fred Bloggs</option>
  <option value="3">Amanda Fredericks</option>
  <option value="4" selected="selected">Natalie Sargos</option>

which renders as:

Please report any errors on this page to the Developer Documentation Forum.

Last Updated ( Saturday, 03 September 2005 )