make html_options work with optgroup, make func modular and recursive.

This commit is contained in:
mohrt
2002-11-01 15:18:27 +00:00
parent f859b613f7
commit e574890ea8
2 changed files with 54 additions and 30 deletions

View File

@@ -21,26 +21,17 @@ function smarty_function_html_options($params, &$smarty)
if (isset($options)) {
settype($options, 'array');
foreach ($options as $key => $value) {
$html_result .= "<option value=\"$key\"";
if (in_array($key, $selected))
$html_result .= " selected=\"selected\"";
$html_result .= ">$value</option>\n";
$html_result .= smarty_function_html_options_optoutput($key, $value, $selected);
}
} else {
settype($output, 'array');
settype($values, 'array');
for ($i = 0, $for_max = count($output); $i < $for_max; $i++) {
/* By default, check value against $selected */
$sel_check = $values[$i];
$html_result .= "<option";
if ($i < count($values))
$html_result .= " value=\"".$values[$i]."\"";
else
$sel_check = $output[$i]; /* if more outputs than values, then
check output against $selected */
if (in_array($sel_check, $selected))
$html_result .= " selected=\"selected\"";
$html_result .= ">".$output[$i]."</option>\n";
if ($i < count($values)) {
$html_result .= smarty_function_html_options_optoutput($values[$i], $output[$i], $selected);
} else {
$html_result .= smarty_function_html_options_optoutput($output[$i], $output[$i], $selected);
}
}
}
@@ -50,6 +41,27 @@ function smarty_function_html_options($params, &$smarty)
return $html_result;
}
function smarty_function_html_options_optoutput($key, $value, $selected) {
if(!is_array($value)) {
$html_result = "<option value=\"$key\"";
if (in_array($key, $selected))
$html_result .= " selected=\"selected\"";
$html_result .= ">$value</option>\n";
} else {
$html_result = smarty_function_html_options_optgroup($key, $value, $selected);
}
return $html_result;
}
function smarty_function_html_options_optgroup($key, $values, $selected) {
$optgroup_html = "<optgroup label=\"$key\">\n";
foreach ($values as $key => $value) {
$optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
}
$optgroup_html .= "</optgroup>\n";
return $optgroup_html;
}
/* vim: set expandtab: */
?>

View File

@@ -21,26 +21,17 @@ function smarty_function_html_options($params, &$smarty)
if (isset($options)) {
settype($options, 'array');
foreach ($options as $key => $value) {
$html_result .= "<option value=\"$key\"";
if (in_array($key, $selected))
$html_result .= " selected=\"selected\"";
$html_result .= ">$value</option>\n";
$html_result .= smarty_function_html_options_optoutput($key, $value, $selected);
}
} else {
settype($output, 'array');
settype($values, 'array');
for ($i = 0, $for_max = count($output); $i < $for_max; $i++) {
/* By default, check value against $selected */
$sel_check = $values[$i];
$html_result .= "<option";
if ($i < count($values))
$html_result .= " value=\"".$values[$i]."\"";
else
$sel_check = $output[$i]; /* if more outputs than values, then
check output against $selected */
if (in_array($sel_check, $selected))
$html_result .= " selected=\"selected\"";
$html_result .= ">".$output[$i]."</option>\n";
if ($i < count($values)) {
$html_result .= smarty_function_html_options_optoutput($values[$i], $output[$i], $selected);
} else {
$html_result .= smarty_function_html_options_optoutput($output[$i], $output[$i], $selected);
}
}
}
@@ -50,6 +41,27 @@ function smarty_function_html_options($params, &$smarty)
return $html_result;
}
function smarty_function_html_options_optoutput($key, $value, $selected) {
if(!is_array($value)) {
$html_result = "<option value=\"$key\"";
if (in_array($key, $selected))
$html_result .= " selected=\"selected\"";
$html_result .= ">$value</option>\n";
} else {
$html_result = smarty_function_html_options_optgroup($key, $value, $selected);
}
return $html_result;
}
function smarty_function_html_options_optgroup($key, $values, $selected) {
$optgroup_html = "<optgroup label=\"$key\">\n";
foreach ($values as $key => $value) {
$optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
}
$optgroup_html .= "</optgroup>\n";
return $optgroup_html;
}
/* vim: set expandtab: */
?>