Files
Form-Laporan-Operasi-F5/lib/functions.php
2026-03-13 11:03:53 +07:00

37 lines
1.7 KiB
PHP

<?php
if (!function_exists('form_dropdown')) {
function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '', $callback = null)
{
if (!is_array($selected)) {
$selected = array($selected);
}
// If no selected state was submitted we will attempt to set it automatically
if (count($selected) === 0) {
// If the form name appears in the $_POST array we have a winner!
if (isset($_POST[$name])) {
$selected = array($_POST[$name]);
}
}
if ($extra != '') $extra = ' ' . $extra;
$multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
$form = '<select name="' . $name . '"' . $extra . $multiple . ">\n";
foreach ($options as $key => $val) {
$key = (string)$key;
if (is_array($val) && !empty($val)) {
$form .= '<optgroup label="' . $key . '">' . "\n";
foreach ($val as $optgroup_key => $optgroup_val) {
$sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
$form .= '<option value="' . $optgroup_key . '"' . $sel . '>' . (string)$optgroup_val . "</option>\n";
}
$form .= '</optgroup>' . "\n";
} else {
$val = ($callback != '') ? $callback($val) : $val;
$sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
$form .= '<option value="' . $key . '"' . $sel . '>' . (string)$val . "</option>\n";
}
}
$form .= '</select>';
return $form;
}
}
?>