Page 1 of 1

Efficiently setting select="select" on date forms with PHP?

Posted: Wed Sep 03, 2008 2:46 am
by JAB Creations
I've got select fields for month and day though I'm not sure how to efficiently set the selected="selected" attribute and value for the select options.

In example my birthday is in November so I would select the November options. I obviously want to have PHP set selected="selected" for the November option.

I presume this may use a loop?

Also a tricky question, would I use an array to replace the month numbers with the appropriate month names? This is the stuff where my head starts to hurt (and I can't seem to find on Google). :|

Re: Efficiently setting select="select" on date forms with PHP?

Posted: Wed Sep 03, 2008 3:08 am
by JAB Creations
Code always helps...I suppose I could simply do an if statement though when it comes to the number on the date select menu it seems a bit excessive...is this what most people do? Is there something better then what most people do however? :P

Code: Select all

<select>
<?php
$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novemeber", "December");
foreach($months as $month)
{
echo '<option value="'.$month.'">'.$month.'</option>';
}
?>
</select>

Re: Efficiently setting select="select" on date forms with PHP?

Posted: Wed Sep 03, 2008 5:16 am
by JAB Creations
Here is what I have, recommendations on improvements?

Code: Select all

// MONTH
echo '<select class="date left" name="month" id="month">';
$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novemeber", "December");
foreach($months as $month) {echo '<option'; if ($month == $row['birth_month']) {echo ' selected="selected"';} echo ' value="'.$month.'">'.$month.'</option>'."\n";}
echo '</select>
<select class="date left" name="day">'."\n";
 
// DAY
$num = 1;
while ($num <=31)
{
 echo '<option';
 if ($num == $row['birth_day']) {echo ' selected="selected"';}
 echo ' value="'.$num.'">'.$num.'</option>'."\n";
 $num++;
} 
echo '</select>';
 

Re: Efficiently setting select="select" on date forms with PHP?

Posted: Wed Sep 03, 2008 6:14 am
by jayshields
Well, for month you could improve it by getting rid of your array and using something like this:

Code: Select all

echo '<select name="month">';
 
for($x = 1; $x <= 12; $x++)
{
    echo '<option value="' . $x . '"';
    if($existingMonth == $x) echo ' selected="selected"';
    echo '>' . date('F', mktime(0, 0, 0, $x)) . '</option>';
}
 
echo '</select>';

Re: Efficiently setting select="select" on date forms with PHP?

Posted: Wed Sep 03, 2008 6:36 am
by JAB Creations
Wow ok didn't expect anything like that, thanks! :mrgreen:

Re: Efficiently setting select="select" on date forms with PHP?

Posted: Wed Sep 03, 2008 1:36 pm
by starram
If you wish to display months like January, February and so on.

You can do two things.

1. You can set a two dimentional array like this:

$month= array("1"=>January, "2"=>February );
and so on...

then run a loop from 1 to 12

for($i=1;$i<1;$i++)
{
<option <? if($i==$_POST['month']) echo "selected"; ?> ><?=$month[$i][0]?></option>
}

2. Secondly you can do the same thing by entering the month values in Databse and fetching records from there and printing in select box options.

And match the condition

<option <? if($databse[monthid]==$_POST['month']) echo "selected"; ?> ><?=$month[$i][0]?></option>

Re: Efficiently setting select="select" on date forms with PHP?

Posted: Wed Sep 03, 2008 9:52 pm
by JAB Creations
Thanks to both of you for going above and beyond. It's one of those threads I'll check back on in the future and I'm sure those searching online for the same thing will appreciate your posts! :)