From c65980879a990e6d02a8ab262b014e670f314477 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 24 May 2021 18:04:18 +0200 Subject: Add function to generate selection boxes. --- includes/view-helpers/select_field.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 includes/view-helpers/select_field.php (limited to 'includes/view-helpers') diff --git a/includes/view-helpers/select_field.php b/includes/view-helpers/select_field.php new file mode 100644 index 0000000..7cff3ef --- /dev/null +++ b/includes/view-helpers/select_field.php @@ -0,0 +1,26 @@ + +// SPDX-FileCopyrightText: 2021 Harald Eilertsen +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +namespace EternalTerror\ViewHelpers; + +/** + * Return HTML code for a selction box with the given options and preselected value. + * + * @param string $name The name attribute for the selection box + * @param array $opts The options as arrays of [value, label] pairs + * @param mixed|int $selected The value of the preselected option, or null if no + * option is preselected. + * @return string + */ +function select_field(string $name, ?array $opts = [], $selected = null) : string +{ + $body = ''; + foreach ($opts as $opt) { + $sel = selected($selected, $opt[0], false); + $body .= ""; + } + return ""; +} -- cgit v1.2.3 From 9694c61f301989cdbbfd9e303e74b30812752cab Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 24 May 2021 19:33:07 +0200 Subject: Allow custom text for no selection in select_fields. --- includes/view-helpers/select_field.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'includes/view-helpers') diff --git a/includes/view-helpers/select_field.php b/includes/view-helpers/select_field.php index 7cff3ef..816f8ef 100644 --- a/includes/view-helpers/select_field.php +++ b/includes/view-helpers/select_field.php @@ -13,11 +13,17 @@ namespace EternalTerror\ViewHelpers; * @param array $opts The options as arrays of [value, label] pairs * @param mixed|int $selected The value of the preselected option, or null if no * option is preselected. + * @param string $blank Text to use for "no selection", defaults to "Please + * select..." * @return string */ -function select_field(string $name, ?array $opts = [], $selected = null) : string +function select_field( + string $name, + ?array $opts = [], + $selected = null, + string $blank = "Please select...") : string { - $body = ''; + $body = ""; foreach ($opts as $opt) { $sel = selected($selected, $opt[0], false); $body .= ""; -- cgit v1.2.3