0
The buildSelect() function takes two arguments (one for the list, and one for options) and builds a selectbox for your form. The first array is simply the list you want to display. There are several examples within the script, so syntax shouldn't be a hassle. The second array is simply an options list. The first two elements are for adding an optional 'Title' to the beginning of the list, and whether is's selected by default. The third, and any subsequent elements of the options array, for that matter, choose which elements of the list are selected ( more than one can be applied to multiple selectboxes) on display
<html>
<head>
<title>PHP select box constructor demo</title>
</head>
<? // build arrays for select boxes
$one = array(
"1" => "Choice one",
"2" => "Choice two",
"3" => "Choice three"
);
$two = array(
"one" => "#1",
"two" => "#2",
"three" => "#3"
);
$three = array(
"T" => "Top",
"M" => "Middle",
"B" => "Bottom"
);
$four = array (
"grla" => "Gorilla",
"ostr" => "Ostritch",
"pthn" => "Python",
"shrk" => "Shark",
"spdr" => "Spider"
);
function buildSelect($selArr, $selOpt){
$selectText = $selOpt[0];
if ($selectText != "") {
$selDefault = "selected";
if ($selOpt[1] == "true") {
$selDefault = "";
}
echo "<option $selDefault>$selectText</option>n";
}
foreach($selArr as $key => $value){
$sel = "";
for ($loop = 2; $loop < count($selOpt); $loop++) {
if ($selOpt[$loop] == $key) {
$sel = "selected";
}
}
echo "<option $sel value='$key'>$value</option>n";
}
return;
}
?>
<body>
<form method="POST" action="">
<table bordercolor="#000000" cellspacing="1" cellpadding="2" border="1">
<tr>
<td class="left">Select Box #1:</td>
<td class="right">
<select name="one">
<? buildSelect($one, array("Select an option", "true", "")) ?>
</select>
</td>
</tr>
<tr>
<td class="left">Select Box #2:</td>
<td class="right">
<select name="two">
<? buildSelect($two, array("Pick one", "false", "")) ?>
</select>
</td>
</tr>
<tr>
<td class="left">Select Box #3:</td>
<td class="right">
<select name="three">
<? buildSelect($three, array("", "", "M")) ?>
</select>
</td>
</tr>
<tr>
<td class="left">Select Box #4:</td>
<td class="right">
<select name="four" multiple size="<?echo count($four) - 2 ?>">
<?
$options = array( "", "", "grla", "pthn");
buildSelect($four, $options)
?>
</select>
</td>
</tr>
</table>
</form>
</body>
</html>
Labels:
PHP
Loading related posts...
PHP
8/29/2010 07:55:00 PM
Subscribe to:
Post Comments (Atom)