create option trait update: value can be array already

This commit is contained in:
Билай Станислав 2024-11-28 13:18:58 +03:00
parent ddb17cc473
commit 45e57367d3

View File

@ -12,10 +12,18 @@ trait CreateOption
public function createOption(array $options, $value = null): string public function createOption(array $options, $value = null): string
{ {
$optionsString = ""; $optionsString = "";
foreach ($options as $val => $title){ if (is_array($value)) {
$selected = (int)$value === $val ? "selected" : ""; foreach ($options as $val => $title) {
$optionsString .= "<option $selected value='$val'>$title</option>"; $selected = in_array($title, $value) ? "selected" : "";
$optionsString .= "<option $selected value='$val'>$title</option>";
}
} else {
foreach ($options as $val => $title) {
$selected = (int)$value === $val ? "selected" : "";
$optionsString .= "<option $selected value='$val'>$title</option>";
}
} }
return $optionsString; return $optionsString;
} }
} }