summaryrefslogtreecommitdiffstats
path: root/includes/view-helpers/select_field.php
blob: 7cff3ef0609e686ee6d152fbff1b41238250ba45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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>";
}