<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>dynamische Auswahlliste generieren</title>
<?php
error_reporting(-1);
?>
</head>
<body>
<?php
function dynAuswahl(
$id, // Die ID des Select
$name, // Der Name des Select
$options, // Die Optionen des Select (array)
$multiple // Gibt an, ob mehrere Optionen auswählbar sind (boolean)
) {
if ($multiple) //Abfrage ob $multiple true ist
{
$multAttribute = " multiple";//belegt die Variable mit multiple, wenn vorhanden, true
$nameAttribute = $name . '[]';//belegt die Variable mit der Variable $name(legst du es in eine Array?)
if($multAttribute){//Wenn true, soll er die Werte, später, auszählen
var_dump(count($multAttribute));//Ein Versuch, zu prüfen ob Werte zum berechnen da sind.
}
} else {
$multAttribute = '';//alternativ wird die Variable mit Null gefüllt, false
$nameAttribute = $name;//alternativ wird die Variable mit der Variable $name gefüllt
}
$html = '<select id="' . $id . '" name="' . $nameAttribute . '"' . $multAttribute . '>';
foreach ($options as $opt) {
$html .= '<option value="' . $opt . '">' . $opt . '</option>';
var_dump(count($options));//Ein Versuch, zu prüfen ob Werte zum berechnen da sind.
}
$html .= '</select>';
return $html;
}
?>
<h1>Einkaufsliste:</h1>
<form method="post">
<p>
<select $id="dynamisch1" name="auswahl1[]" size="" multiple"">
<option value="Brot"> Brot </option>
<option value="Butter"> Butter </option>
<option value="Milch"> Milche </option>
<option value="Eier"> Eier </option>
<option value="Käse"> Käse </options>
<option value="Wurst"> Wurst </option>
</select>
</p>
<p>
<select id="dynamisch2" name="auswahl2[]" size="<?php echo $arrZahl; ?>" multiple="multiple">
<option value="Schrauben"> Schrauben </option>
<option value="Nägel"> Nägel </option>
<option value="Haken"> Haken </option>
<option value="Nadeln"> Nadeln </option>
<option value="Dübel"> Dübel </option>
</select>
</p>
<p>
<input type="submit" value="Abschicken">
</p>
</form>
</body>
</html>