PHP - Auswahlliste aus einem Array erstellen
<?php
// Auswahlliste aus einem Array erstellen
function select($options = [], $selected = null) {
$html = '';
if (sizeof($options) > 0) {
$html = '<select name="selection">' . PHP_EOL;
foreach($options as $value => $title) {
if ($value === $selected) {
$selectedAttr = ' selected';
} else {
$selectedAttr = '';
}
$html .= ' <option value="' . $value . '"' . $selectedAttr . '>' . $title . '</option>' . PHP_EOL;
}
$html .= '</select>';
}
return $html;
}
$options = array(1 => 'Apfel', 2 => 'Birne', 3 => 'Pflaume');
$html = select($options, 2);
echo $html;
?>
Siehe auch: Auswahlliste aus dem Inhalt einer DB-Spalte erstellen
Bausteine Alle Anzeigen
Eine zufällige Auswahl von Codeschnipseln
aus den Bereichen
HTML, CSS,
PHP, JavaScript und
MySQL.
<fieldset> Formularelemente gruppieren
CSS - Tastenkombinationen darstellen
PHP - Inhalt immer frisch vom Server holen