I have a form consisting of 3 dropdowns next to text fields, ie:
Label: Textfield Select
Time1: 2 Days
Time2: 2 Hours
Time3: 2 Months
(the option 'Week' is also available')
I can go so far as to determine that Time1 has Day selected, Time2 has Hours selected, and Time3 has Months selected, like so:
Array ( [0] => hour [1] => day [2] => month )
I then want to compare apples to apples by converting days, months, and weeks into hours (and eventually back again), so now I want to dynamically determine that Time1 (array 0) is associated with Time1, so textfield must now be 224 to equal 48 hours, while Time3 (array 2) must be 2730.484398.
Here is what I have so far; how can this be better written?
$time = array("hour" => "hour", "day" => "day", "month" => "month", "week" => "week");
$timeO = $time;
$timeP = $time;
$timeM = $time;
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;
}
?>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<form id="timeEstimate" action="<? echo $PHP_SELF; ?>" method="post" name="timeEstimate">
Optimistic: <input type="text" name="To" size="24">
<select name="timeO">
<? buildSelect($timeO, array("Select an option", "true", "")) ?>
</select> <br>
Pessimistic: <input type="text" name="Tp" size="24">
<select name="timeP">
<? buildSelect($timeP, array("Select an option", "true", "")) ?>
</select> <br>
Most Likely: <input type="text" name="Tm" size="24">
<select name="timeM">
<? buildSelect($timeM, array("Select an option", "true", "")) ?>
</select> <br>
<input type="submit" name="Submit">
<hr>
<?
if (Submit) {
$To = $_POST['To'];
$Tp = $_POST['Tp'];
$Tm = $_POST['Tm'];
$timeO = $_POST['timeO'];
$timeP = $_POST['timeP'];
$timeM = $_POST['timeM'];
$times = array($timeO, $timeP, $timeM);
print_r($times);
echo "<BR>";
foreach ($times as $value) {
$day = array_search('day', $times);
$hour = array_search('hour', $times);
$week = array_search('week', $times);
$month = array_search('month', $times);
}