select() public method

### Attributes: - showParents - If included in the array and set to true, an additional option element will be added for the parent of each option group. You can set an option with the same name and it's key will be used for the value of the option. - multiple - show a multiple select box. If set to 'checkbox' multiple checkboxes will be created instead. - empty - If true, the empty select option is shown. If a string, that string is displayed as the empty element. - escape - If true contents of options will be HTML entity encoded. Defaults to true. - value The selected value of the input. - class - When using multiple = checkbox the class name to apply to the divs. Defaults to 'checkbox'. - disabled - Control the disabled attribute. When creating a select box, set to true to disable the select box. When creating checkboxes, true will disable all checkboxes. You can also set disabled to a list of values you want to disable when creating checkboxes. ### Using options A simple array will create normal options: $options = array(1 => 'one', 2 => 'two); $this->Form->select('Model.field', $options)); While a nested options array will create optgroups with options inside them. $options = array( 1 => 'bill', 'fred' => array( 2 => 'fred', 3 => 'fred jr.' ) ); $this->Form->select('Model.field', $options); In the above 2 => 'fred' will not generate an option element. You should enable the showParents attribute to show the fred option. If you have multiple options that need to have the same value attribute, you can use an array of arrays to express this: $options = array( array('name' => 'United states', 'value' => 'USA'), array('name' => 'USA', 'value' => 'USA'), );
public select ( string $fieldName, array $options = [], array $attributes = [] ) : string
$fieldName string Name attribute of the SELECT
$options array Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the SELECT element
$attributes array The HTML attributes of the select element.
return string Formatted SELECT element
Example #1
0
 /**
  * select プルダウンメニューを表示
  * 
  * @param	string $fieldName フィールド文字列
  * @param	array $options コントロールソース
  * @param	array $attributes html属性
  * - $attributes['cols']が指定されている場合、値の文字の横幅を指定できる 
  * @param	array	空データの表示有無
  * @return	string $showEmpty htmlタグ
  * @access	public
  */
 public function select($fieldName, $options = array(), $attributes = array())
 {
     if ($this->freezed) {
         return $this->freezeControll($fieldName, $options, $attributes);
     } else {
         // 横幅を設定する
         // 指定した文字数より足りない文字数分スペースを埋める処理としている為、
         // 等幅フォントを設定しないとちゃんとした横幅にはならない
         if (!empty($attributes['cols'])) {
             foreach ($options as $key => $option) {
                 if ($attributes['cols'] > mb_strlen($option)) {
                     $pad = str_repeat(' ', $attributes['cols'] - mb_strlen($option));
                     $options[$key] = $option . $pad;
                 }
             }
         }
         return parent::select($fieldName, $options, $attributes);
     }
 }