form_dropdown()

form_dropdown([$name = ''[, $options = array()[, $selected = array()[, $extra = '']]]])

Parameters:
  • $name (string) – Field name
  • $options (array) – An associative array of options to be listed
  • $selected (array) – List of fields to mark with the selected attribute
  • $extra (mixed) – Extra attributes to be added to the tag either as an array or a literal string
Returns:

An HTML dropdown select field tag

Return type:

string

Lets you create a standard drop-down field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected. You can also pass an array of multiple items through the third parameter, and CodeIgniter will create a multiple select for you.

Example:

$options = array(
        'small'         => 'Small Shirt',
        'med'           => 'Medium Shirt',
        'large'         => 'Large Shirt',
        'xlarge'        => 'Extra Large Shirt',
);

$shirts_on_sale = array('small', 'large');
echo form_dropdown('shirts', $options, 'large');

/*
        Would produce:

        <select name="shirts">
                <option value="small">Small Shirt</option>
                <option value="med">Medium  Shirt</option>
                <option value="large" selected="selected">Large Shirt</option>
                <option value="xlarge">Extra Large Shirt</option>
        </select>
*/

echo form_dropdown('shirts', $options, $shirts_on_sale);

/*
        Would produce:

        <select name="shirts" multiple="multiple">
                <option value="small" selected="selected">Small Shirt</option>
                <option value="med">Medium  Shirt</option>
                <option value="large" selected="selected">Large Shirt</option>
                <option value="xlarge">Extra Large Shirt</option>
        </select>
*/

If you would like the opening <select> to contain additional data, like an id attribute or JavaScript, you can pass it as a string in the fourth parameter:

$js = 'id="shirts" onChange="some_function();"';
echo form_dropdown('shirts', $options, 'large', $js);

Or you can pass it as an array:

$js = array(
        'id'       => 'shirts',
        'onChange' => 'some_function();'
);
echo form_dropdown('shirts', $options, 'large', $js);

If the array passed as $options is a multidimensional array, then form_dropdown() will produce an <optgroup> with the array key as the label.

doc_CodeIgniter
2016-10-15 16:32:18
Comments
Leave a Comment

Please login to continue.