summaryrefslogtreecommitdiffstats
path: root/includes/view-helpers/select_field.php
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2021-05-24 18:04:18 +0200
committerHarald Eilertsen <haraldei@anduin.net>2021-05-24 18:04:18 +0200
commitc65980879a990e6d02a8ab262b014e670f314477 (patch)
tree976065c2fe52760bc69977b9c9d668af0e30c78d /includes/view-helpers/select_field.php
parentae72f1b2f0c73ded5277300f0d15914e6e10ecae (diff)
downloadgigologadmin-c65980879a990e6d02a8ab262b014e670f314477.tar.gz
gigologadmin-c65980879a990e6d02a8ab262b014e670f314477.tar.bz2
gigologadmin-c65980879a990e6d02a8ab262b014e670f314477.zip
Add function to generate selection boxes.
Diffstat (limited to 'includes/view-helpers/select_field.php')
-rw-r--r--includes/view-helpers/select_field.php26
1 files changed, 26 insertions, 0 deletions
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 @@
+<?php
+// SPDX-FileCopyrightText: 2021 Andrea Chirulescu <andrea.chirulescu@gmail.com>
+// SPDX-FileCopyrightText: 2021 Harald Eilertsen <haraldei@anduin.net>
+//
+// 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 .= "<option value=\"{$opt[0]}\"{$sel}>{$opt[1]}</option>";
+ }
+ return "<select name=\"{$name}\">{$body}</select>";
+}