Help me make this code more efficient
Posted: Mon Aug 19, 2002 5:44 pm
I'm passing variables for an event's start and end times from a form with three fields for each time (hour, minute, and am/pm). On the current page I'm validating that the start time is prior to the end time. If the time is not valid I want to display the form fields again with the selected value being the time variables passed originally (so users can see the erroneous times they entered).
Here are my POST variables:
$Start_HOUR,$Start_MIN,$Start_AMPM
$End_HOUR,$End_MIN,$End_AMPM
Since the form fields are identical for start and end times (except for the variable names), I don't want to have to list them twice, but I can't figure out how to get around it. You'll also see a few other inefficiencies around the selected MIN and AMPM.
Then I repeat all this for the End Time.
I know there's a better way. Any help would be appreciated. Ruth
Here are my POST variables:
$Start_HOUR,$Start_MIN,$Start_AMPM
$End_HOUR,$End_MIN,$End_AMPM
Since the form fields are identical for start and end times (except for the variable names), I don't want to have to list them twice, but I can't figure out how to get around it. You'll also see a few other inefficiencies around the selected MIN and AMPM.
Code: Select all
/* Use drop down lists to select the start and end times */
echo
"Start Time:<br>\n<td>";
/* Drop down list for the hour */
for ($n=1; $n<=12; $n++)
{
echo "<option value=$n";
if ($n == '$Start_HOUR')
{
echo " selected";
}
echo ">$n\n";
}
echo "</SELECT>\n";
/* Drop down list for the minute */
echo "<select name= '$Start_MIN'>\n";
echo "<option value='00'>00\n
<option value='15'";
if ('$Start_MIN' == 15)
{
echo " selected";
}
echo ">15\n
<option value='30'";
if ('$Start_MIN' == 30)
{
echo " selected";
}
echo ">30\n
<option value='45'";
if ('$Start_MIN' == 45)
{
echo " selected";
}
echo ">45\n
</SELECT>\n";
/* Drop down list for AM/PM */
echo "<select name= $value.'_AMPM'>\n";
echo "<option value='AM'";
if ('$Start_AMPM' == 'AM')
{
echo " selected";
}
echo ">AM\n
<option value='PM'";
if ('$Start_AMPM' == 'AM')
{
echo " selected";
}
echo ">PM\n
</SELECT><p>\n";
}I know there's a better way. Any help would be appreciated. Ruth