[RESOLVED] Most efficiant way to handle input of "selected" in form
lets say you have a form, each entry hold a month, example:
Code:
<option value='1'>January</option><option value='2'>February</option><option value='3'>March</option><option value='4' SELECTED>April</option><option value='5'>May</option><option value='6'>June</option><option value='7'>July</option><option value='8'>August</option><option value='9'>September</option><option value='10'>October</option><option value='11'>November</option><option value='12'>December</option>
I then have a $CurrentMonth which holds the int value for the month that I want selected.
Is there some trick to doing this effectly of do i simple do something like:
echo start of data
check if check should be there
echo end of data
then do that for each month... (ive done this before but it just seem VERY inefficiant and messy)
edit: this is what I currently have.... problem is its very bad code... and I also got 2 do this with the year (and there are many more then 12 events).... Maybe it can be done with a array or something...
Code:
if (intval($vMonth) == 1) $DateInput.= " SELECTED";
$DateInput.= ">January</option><option value='2'";
if (intval($vMonth) == 2) $DateInput.= " SELECTED";
$DateInput.=">February</option><option value='3'";
if (intval($vMonth) == 3) $DateInput.= " SELECTED";
$DateInput.=">March</option><option value='4'";
if (intval($vMonth) == 4) $DateInput.= " SELECTED";
$DateInput.=">April</option><option value='5'";
if (intval($vMonth) == 5) $DateInput.= " SELECTED";
$DateInput.=">May</option><option value='6'";
if (intval($vMonth) == 6) $DateInput.= " SELECTED";
$DateInput.=">June</option><option value='7'";
if (intval($vMonth) == 7) $DateInput.= " SELECTED";
$DateInput.=">July</option><option value='8'";
if (intval($vMonth) == 8) $DateInput.= " SELECTED";
$DateInput.=">August</option><option value='9'";
if (intval($vMonth) == 9) $DateInput.= " SELECTED";
$DateInput.=">September</option><option value='10'";
if (intval($vMonth) == 10) $DateInput.= " SELECTED";
$DateInput.=">October</option><option value='11'";
if (intval($vMonth) == 11) $DateInput.= " SELECTED";
$DateInput.=">November</option><option value='12'";
if (intval($vMonth) == 12) $DateInput.= " SELECTED";
$DateInput.=">December</option>";
Re: Most efficiant way to handle input of "selected" in form
I don't know php enough to tell put this in correct format but it could be done with an array of all the months. Then a loop to populate the control would be
Code:
for iLoop = 1 to 12
$DateInput .= "<option value='".iLoop."'";
if (intval($vMonth) == iLoop) $DateInput.=" SELECTED";
$DateInput.=">".arrMonth(iLoop)."</option>;"
next
Re: Most efficiant way to handle input of "selected" in form
Quote:
Originally Posted by schaefer
I don't know php enough to tell put this in correct format but it could be done with an array of all the months. Then a loop to populate the control would be
Code:
for iLoop = 1 to 12
$DateInput .= "<option value='".iLoop."'";
if (intval($vMonth) == iLoop) $DateInput.=" SELECTED";
$DateInput.=">".arrMonth(iLoop)."</option>;"
next
yeah I knew I was brain farting :P
Code:
for ( $counter = 0; $counter <= (count($vaMonthList)-1); $counter++)
{
$DateInput.= "<option value='" . ($counter+1) . "'";
if (intval($vMonth) == ($counter+1)) $DateInput.= " SELECTED";
$DateInput.= ">" . $vaMonthList[$counter] . "</option>";
}