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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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). :|
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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>
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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>';
 
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

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

Post 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>';
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post by JAB Creations »

Wow ok didn't expect anything like that, thanks! :mrgreen:
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

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

Post 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>
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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! :)
Post Reply