-
Code's Tags
-
Your Codes
-
Reffers
-
Linked Codes
|
Code:
Short link for Twitter:
HTML:
HTML view:
Copy Source | Copy HTML /** * getopts() by sergey.shuchkin[ att ]gmail.com * generate html options: * - understand key => value array * - understand 2d arrays (from DB) * - understand 2d array of objects * - multiple selection * - escape values & labels */ function getopts($items, $selected = NULL) { if (!is_array($items)) return ''; list (,$val) = each($items); if (is_array($val)) { // convert DB array $a = array(); foreach ($items as $k => $v) { list(,$val) = each($v); // 1 value list(,$lbl) = each($v); // 2 label $a[$val] = $lbl; } $items = &$a; } if (is_object($val)) { // convert DB objects $a = array(); foreach ($items as $k => $v) { $p = get_object_vars( $v ); list(,$val) = each($p); // 1 value list(,$lbl) = each($p); // 2 label $a[$val] = $lbl; } $items = &$a; } $opts = ''; foreach ($items as $k => $v) { if (is_array($selected)) { // to use in multiple listbox $flag = in_array($k,$selected); } else if (!is_null($selected)) { $flag = $k == $selected; } else $flag = false; $opts .= ' <option value="'.htmlspecialchars($k, ENT_QUOTES, 'UTF-8').'"'.($flag ? ' selected="selected"' : '').'>'.strip_tags($v).'</option>'."\n"; } return $opts; }
|