<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>dynamische Auswahlliste generieren</title>
<?php
error_reporting(-1);
ini_set("display_errors", 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){
$arrZahl = count($options);
} else {
$arrZahl = 1;
}
} 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' sitze='$arrZahl' multiple='$multAttribute'>\n";
foreach ($options as $opt) {
$html .= "<option value='$opt'> $opt </option>\n";
}
$html .= "</select>";
return $html;
}
?>
<h1>Einkaufsliste PHP</h1>
<form method="post">
<p>
<?php
echo dynAuswahl("dynamisch1", "auswahl1", array("Brot", "Butter", "Milch", "Eier", "Käse", "Wurst"), false);
?>
</p>
<p>
<?php
echo dynAuswahl("dynamisch2", "auswahl2", array("Schrauben", "Nägel", "Haken", "Nadeln", "Dübel"), true);
?>
</p>
<p>
<input type="submit" value="Abschicken">
</p>
<br>
<hr>
<h1>Einkaufsliste HTML</h1>
<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="" 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>