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
{
$optionsString = "";
foreach ($options as $val => $title){
$selected = (int)$value === $val ? "selected" : "";
$optionsString .= "<option $selected value='$val'>$title</option>";
if (is_array($value)) {
foreach ($options as $val => $title) {
$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;
}
}