mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-04 18:34:27 +02:00
make html_options work with optgroup, make func modular and recursive.
This commit is contained in:
@@ -21,26 +21,17 @@ function smarty_function_html_options($params, &$smarty)
|
|||||||
if (isset($options)) {
|
if (isset($options)) {
|
||||||
settype($options, 'array');
|
settype($options, 'array');
|
||||||
foreach ($options as $key => $value) {
|
foreach ($options as $key => $value) {
|
||||||
$html_result .= "<option value=\"$key\"";
|
$html_result .= smarty_function_html_options_optoutput($key, $value, $selected);
|
||||||
if (in_array($key, $selected))
|
|
||||||
$html_result .= " selected=\"selected\"";
|
|
||||||
$html_result .= ">$value</option>\n";
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
settype($output, 'array');
|
settype($output, 'array');
|
||||||
settype($values, 'array');
|
settype($values, 'array');
|
||||||
for ($i = 0, $for_max = count($output); $i < $for_max; $i++) {
|
for ($i = 0, $for_max = count($output); $i < $for_max; $i++) {
|
||||||
/* By default, check value against $selected */
|
if ($i < count($values)) {
|
||||||
$sel_check = $values[$i];
|
$html_result .= smarty_function_html_options_optoutput($values[$i], $output[$i], $selected);
|
||||||
$html_result .= "<option";
|
} else {
|
||||||
if ($i < count($values))
|
$html_result .= smarty_function_html_options_optoutput($output[$i], $output[$i], $selected);
|
||||||
$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";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,6 +41,27 @@ function smarty_function_html_options($params, &$smarty)
|
|||||||
return $html_result;
|
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: */
|
/* vim: set expandtab: */
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
@@ -21,26 +21,17 @@ function smarty_function_html_options($params, &$smarty)
|
|||||||
if (isset($options)) {
|
if (isset($options)) {
|
||||||
settype($options, 'array');
|
settype($options, 'array');
|
||||||
foreach ($options as $key => $value) {
|
foreach ($options as $key => $value) {
|
||||||
$html_result .= "<option value=\"$key\"";
|
$html_result .= smarty_function_html_options_optoutput($key, $value, $selected);
|
||||||
if (in_array($key, $selected))
|
|
||||||
$html_result .= " selected=\"selected\"";
|
|
||||||
$html_result .= ">$value</option>\n";
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
settype($output, 'array');
|
settype($output, 'array');
|
||||||
settype($values, 'array');
|
settype($values, 'array');
|
||||||
for ($i = 0, $for_max = count($output); $i < $for_max; $i++) {
|
for ($i = 0, $for_max = count($output); $i < $for_max; $i++) {
|
||||||
/* By default, check value against $selected */
|
if ($i < count($values)) {
|
||||||
$sel_check = $values[$i];
|
$html_result .= smarty_function_html_options_optoutput($values[$i], $output[$i], $selected);
|
||||||
$html_result .= "<option";
|
} else {
|
||||||
if ($i < count($values))
|
$html_result .= smarty_function_html_options_optoutput($output[$i], $output[$i], $selected);
|
||||||
$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";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,6 +41,27 @@ function smarty_function_html_options($params, &$smarty)
|
|||||||
return $html_result;
|
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: */
|
/* vim: set expandtab: */
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
Reference in New Issue
Block a user