Exemple #1
0
 public static function dropdown($data, $options = NULL, $selected = NULL, $extra = '')
 {
     if (!is_array($data)) {
         $data = array('name' => $data);
     } else {
         if (isset($data['options'])) {
             // Use data options
             $options = $data['options'];
         }
         if (isset($data['selected'])) {
             // Use data selected
             $selected = $data['selected'];
         }
     }
     if (is_array($selected)) {
         // Multi-select box
         $data['multiple'] = 'multiple';
     } else {
         // Single selection (but converted to an array)
         $selected = array($selected);
     }
     $input = '<select' . joosHTML::attributes($data, 'select') . ' ' . $extra . '>' . "\n";
     foreach ((array) $options as $key => $val) {
         // Key should always be a string
         $key = (string) $key;
         if (is_array($val)) {
             $input .= '<optgroup label="' . $key . '">' . "\n";
             foreach ($val as $inner_key => $inner_val) {
                 // Inner key should always be a string
                 $inner_key = (string) $inner_key;
                 $sel = in_array($inner_key, $selected) ? ' selected="selected"' : '';
                 $input .= '<option value="' . $inner_key . '"' . $sel . '>' . $inner_val . '</option>' . "\n";
             }
             $input .= '</optgroup>' . "\n";
         } else {
             $sel = in_array($key, $selected) ? ' selected="selected"' : '';
             $input .= '<option value="' . $key . '"' . $sel . '>' . $val . '</option>' . "\n";
         }
     }
     $input .= '</select>';
     return $input;
 }