Generate <select> options on the Fly

<example>
fhtml("select @name=country1")->options(["at"=>"Austria", "de" => "Germany", "us" => "USA"], $_POST["country1"]);
</example>

@param array $options
@param string|null $selected
@return FHtml
194851
public function options(array $options, string $selected=null) : self {
        foreach ($options as $key => $value) {
            if (is_int($key))
                $key = $value;
            if ($selected == $key)
                $this->elem("option @value=? @selected=selected", [$key])->text($value)->end();
            else
                $this->elem("option @value=?", [$key])->text($value)->end();
        }
        return $this;
    }
852435
public static function select($name, $options, $selected = NULL, Array $attributes = NULL) {
    $name = self::getName($name);
    
    if (is_string($options)) {
      // nothing, wir haben hier vermutlich schon was zusammengebaut, was so hinkommt
    } else {
      $options = self::selectOptions($options, $selected);
    }
    
    $select = static::tag('select',$options, $attributes)
      ->setAttribute('name',$name)
      ->setGlueContent("%s \n");
    
    return $select;
  }
934019
protected function render_select(): string
    {
        if (!$this->choices) {
            return '';
        }

        $html = '<select '.$this->metaString().' id="'.
            $this->escape->attr($this->id).'" name="'.
            $this->escape->attr($this->name).'">';

        foreach ($this->choices as $value => $label) {
            if (isset($this->meta['multiple'])) {
                $selected = $this->selected(
                    $value,
                    (\in_array($value, (array)$this->value) ? $value : '')
                );
            } else {
                $selected = $this->selected($value, $this->value);
            }

            $html .= '<option '.$selected.' value="'.
                $this->escape->attr($value).'">'.$label.'</option>';
        }

        $html .= '</select>';

        return $html;
    }
1227674
public function select($name, $list = [], $selected = null, array $selectAttributes = [], array $optionsAttributes = [], array $optgroupsAttributes = [])
    {
        $selectAttributes = $this->appendClassToOptions('form-control', $selectAttributes);

        if ($this->hasErrors($name)) {
            $selectAttributes = $this->appendClassToOptions('is-invalid', $selectAttributes);
        }

        return parent::select($name, $list, $selected, $selectAttributes, $optionsAttributes, $optgroupsAttributes);
    }
1052070
public function render(array $options, $name, $selected = array(), array $attrs = array(), $tpl = false)
    {
        $output = array();
        $this->_setTpl($tpl);

        foreach ($options as $value => $label) {
            $label = trim($label);
            $id    = Str::unique($this->_type . '-');
            $value = $this->_cleanValue($value);
            $text  = $this->_translate($label);
            $attrs = $this->_checkedOptions($value, $selected, $attrs);

            $output[] = $this->_elementTpl($name, $value, $id, $text, $attrs);
        }

        return implode(PHP_EOL, $output);
    }
660928
public function select(String $name = NULL, Array $options = [], $selected = NULL, Array $_attributes = [], Bool $multiple = false)
    {
        $this->isTableOrQueryData($options);

        $this->setOptionAttribute($options);

        $this->isExcludeAttribute($options);

        $this->isIncludeAttribute($options);

        $this->isOrderAttribute($options);

        $this->setSelectedAttribute($selected, $options);

        $this->setMultipleAttribute($multiple, $_attributes);

        $this->setNameAttributeWithReference($name, $_attributes);

        if( ! empty($_attributes['name']) )
        {
            $this->_postback($_attributes['name'], $selected);

            # 5.8.2.8[added]
            $this->getVMethodMessages();

            # 5.4.2[added]
            $this->_validate($_attributes['name'], $_attributes['name']);
            
            # 5.4.2[added]|5.4.5|5.4.6[edited]
            $selected = $this->_getrow('select', $selected, $_attributes);
        }

        $this->commonMethodsForInputElements('select');

        $this->getPermAttribute($perm);

        $this->createSelectElement($options, $selected, $_attributes, $return);

        $this->createBootstrapFormInputElementByType('select', $return, $_attributes, $return);

        $this->_unsetselect();

        $this->outputElement .= $this->_perm($perm, $return);

        return $this;
    }
1024617
static function select($name, $option, $default = null, $self = null, $attrArray = null, $add_help = true, $force = false)
	{
		$htmlStr = "<select name=\"$name\" id=\"$name\"";
		$htmlStr .= self::input_attr($attrArray) . ">\n";

        if ($add_help) {
            if ($add_help === true) {
                $htmlStr .= "<option value=\"\">" . self::$default_help_option . "</option>\n";
            } else {
                $htmlStr .= "<option value=\"\">$add_help</option>\n";
            }
        }
        foreach ($option as $key => $value) {
            if ($self) {
                $key = $value;
            }

            if (!$force and $key == $default) {
                $htmlStr .= "<option value=\"{$key}\" selected=\"selected\">{$value}</option>\n";
            } elseif ($force and $key === $default) {
				$htmlStr .= "<option value=\"{$key}\" selected=\"selected\">{$value}</option>\n";
			} else {
                $htmlStr .= "<option value=\"{$key}\">{$value}</option>\n";
            }
        }
        $htmlStr .= "</select>\n";

		return $htmlStr;
	}
603238
public function selectTag($name, $options, array $attrs = [])
    {
        # This is found also in Form::select()
        if (!is_string($options)) {
            if (is_array($options) && ArrayTools::isIndexed($options) && count($options) == 2)
                list ($options, $value) = $options;
            else
                $value = null;
            $options = $this->optionsForSelect($options, $value);
        }
        
        if (isset($attrs['prompt'])) {
            $options = $this->contentTag('option', $attrs['prompt'], ['value' => '', 'allow_blank_attrs' => true]) . "\n" . $options;
            unset($attrs['prompt']);
        }
        $attrs['value'] = $attrs['type'] = null;
        
        $select_tag = $this->_form_field_tag('select', $name, $options, $attrs, true);
        return $select_tag;
    }
378949
public function dropdown($name, $options = [], $atributes = null)
    {
        $ret = '<select name="'.$name.'" '.(($attributes !== null) ? $this->attributes($attributes) : "").'>';
        foreach ($variable as $key => $value) {
            if (is_array($value)) {
                $ret .= '<option value="'.$key.'" '.((isset($value["attributes"])) ? $this->atributes($value["attributes"]) : "").'>'.$this->getValue($key, $value["value"]).'</option>';
            } else {
                $ret .= '<option value="'.$key.'">'.$this->getValue($key, $value).'</option>';
            }
        }
        $ret .= "</select>";
        return $ret;
    }
515734
public function selectTag($name, $optionTags, array $options = [])
    {
        # This is found also in Form::select()
        if (!is_string($optionTags)) {
            if (is_array($optionTags) && isset($optionTags[0]) && isset($optionTags[1])) {
                list ($optionTags, $value) = $optionTags;
            } else {
                $value = null;
            }
            $optionTags = $this->optionsForSelect($optionTags, $value);
        }
        
        $this->normalizeSelectOptions($options, $optionTags);
        
        return $this->formFieldTag('select', $name, $optionTags, $options, true);
    }
144556
public function select($name, $options = array(), $selected = null, array $attributes = array(), array $extra = array())
    {
        /**
         * Swap values so programmers can skip the $value argument
         * and pass the $attributes array directly.
         */
        if (is_array($selected) && empty($attributes)) {
            $extra = $attributes;
            $attributes = $selected;
            $selected = null;
        }

        return $this->doBuild('select', $name, $selected, $attributes, $extra, $options);
    }
215216
protected function createForm($options = null)
    {
        $optionVars = array('askCheck', 'askOld', 'checkFields', 'forceRules', 'labelWidthFactor', 'reportRules');
        foreach ($optionVars as $name) {
            if ($this->$name !== null) {
                $options[$name] = $this->$name;
            }
        }
        $options['useTableLayout'] = false; // Layout set by this snippet

        return $this->user->getChangePasswordForm($options);
    }
347789
public function selectMonth($name, $selected = null, $options = array())
	{
		$months = array();

		foreach (range(1, 12) as $month)
		{
			$months[$month] = strftime('%B', mktime(0, 0, 0, $month, 1));
		}

		return $this->select($name, $months, $selected, $options);
	}
366530
public static function checkboxList($name, $selection = null, $items = [], $options = [])
    {
        if (!isset($options['item'])) {
            $itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
            $encode = ArrayHelper::getValue($options, 'encode', true);
            $options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $encode) {
                $options = array_merge([
                    'label' => $encode ? static::encode($label) : $label,
                    'value' => $value
                ], $itemOptions);
                return '<div class="checkbox">' . Html::checkbox($name, $checked, $options) . '</div>';
            };
        }

        return parent::checkboxList($name, $selection, $items, $options);
    }
22265
public function createOptions(array $options, $selected = null)
    {
        foreach ($options as $label => $value) {
            $option = $this->createOption($label, $value);

            if (is_array($selected) && in_array($value, $selected)) {
                $option->selected();
            } elseif ($selected === $value) {
                $option->selected();
            }
        }

        return $this;
    }
769911
public function select($name, array $list = [], $selected = null, array $options = [])
    {
        return $this->form->select($name,
                                   $list,
                                   $selected,
                                   $this->addFormControlClass($this->getFieldOptions($name, $options)));
    }
73797
public function plainSelect($name, $list = array(), $selected = null, $options = array())
    {
        return parent::select($name, $list, $selected, $options);
    }