选择语言 :

 Core_Form::select

Creates a select form input.

echo Form::select('country', $countries, $country);
string Core_Form::select( string $name [, array $options = null , mixed $selected = null , array $attributes = null ] )
uses
HTML::attributes

参数列表

参数 类型 描述 默认值
$name string Input name
$options array Available options null
$selected mixed Selected option string, or an array of selected options null
$attributes array Html attributes null
返回值
  • string
File: ./core/classes/form.class.php
public static function select($name, array $options = null, $selected = null, array $attributes = null)
{
    // Set the input name
    $attributes['name'] = $name;

    if (is_array($selected))
    {
        // This is a multi-select, god save us!
        $attributes['multiple'] = 'multiple';
    }

    if (!is_array($selected))
    {
        if ($selected === null)
        {
            // Use an empty array
            $selected = array();
        }
        else
        {
            // Convert the selected options to an array
            $selected = array((string)$selected);
        }
    }

    if (empty($options))
    {
        // There are no options
        $options = '';
    }
    else
    {
        foreach ($options as $value => $name)
        {
            if (is_array($name))
            {
                // Create a new optgroup
                $group = array('label' => $value);

                // Create a new list of options
                $_options = array();

                foreach ($name as $_value => $_name)
                {
                    // Force value to be string
                    $_value = (string)$_value;

                    // Create a new attribute set for this option
                    $option = array('value' => $_value);

                    if (in_array($_value, $selected))
                    {
                        // This option is selected
                        $option['selected'] = 'selected';
                    }

                    // Change the option to the HTML string
                    $_options[] = '<option' . HTML::attributes($option) . '>' . HTML::chars($_name, false) . '</option>';
                }

                // Compile the options into a string
                $_options = "\n" . implode("\n", $_options) . "\n";

                $options[$value] = '<optgroup' . HTML::attributes($group) . '>' . $_options . '</optgroup>';
            }
            else
            {
                // Force value to be string
                $value = (string)$value;

                // Create a new attribute set for this option
                $option = array('value' => $value);

                if (in_array($value, $selected))
                {
                    // This option is selected
                    $option['selected'] = 'selected';
                }

                // Change the option to the HTML string
                $options[$value] = '<option' . HTML::attributes($option) . '>' . HTML::chars($name, false) . '</option>';
            }
        }

        // Compile the options into a single string
        $options = "\n" . implode("\n", $options) . "\n";
    }

    return '<select' . HTML::attributes($attributes) . '>' . $options . '</select>';
}