summaryrefslogtreecommitdiffstats
path: root/includes/view-helpers/select_field.php
diff options
context:
space:
mode:
authorAndreaChirulescu <andrea.chirulescu@gmail.com>2021-06-15 18:34:29 +0200
committerAndreaChirulescu <andrea.chirulescu@gmail.com>2021-06-15 18:34:29 +0200
commit1a8155ea64352b0eaf088675d51547361f6f17a7 (patch)
tree7da669474e0896cefbec89a16b65413a7ed25cb1 /includes/view-helpers/select_field.php
parentbfe4eb5efd705b64943a0e1c0b7e18bfe0eee4d8 (diff)
parent82c4a8f2c4e5acd80b813829cecc40f621da3b21 (diff)
downloadgigologadmin-1a8155ea64352b0eaf088675d51547361f6f17a7.tar.gz
gigologadmin-1a8155ea64352b0eaf088675d51547361f6f17a7.tar.bz2
gigologadmin-1a8155ea64352b0eaf088675d51547361f6f17a7.zip
Merge branch 'dev' of https://code.volse.net/wordpress/plugins/gigologadmin.git into andreaschanges
Diffstat (limited to 'includes/view-helpers/select_field.php')
-rw-r--r--includes/view-helpers/select_field.php32
1 files changed, 32 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..816f8ef
--- /dev/null
+++ b/includes/view-helpers/select_field.php
@@ -0,0 +1,32 @@
+<?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.
+ * @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 $blank = "Please select...") : string
+{
+ $body = "<option value=\"\">{$blank}</option>";
+ 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>";
+}