scarhand Posted November 16, 2007 Share Posted November 16, 2007 i am trying to generate a list of months with a simple while loop, but all i'm getting is 12 "December"'s in the select. heres my code: <?php <select name="birthmonth"> <?php $a = 1; while ($a <= 12) { $b = date("F", $a); if ($regbirthmonth == $b) { $selected = 'selected'; } else { $selected = ''; } echo "<option value=\"$b\" $selected>$b</option>"; $a++; } ?> </select> ?> any help would be greatly appreciated Quote Link to comment Share on other sites More sharing options...
metrostars Posted November 16, 2007 Share Posted November 16, 2007 <select name="birthmonth"> <?php $a = 1; while ($a <= 12) { $b = date("F", mktime(0, 0, 0, $a, 1, 0)); if ($regbirthmonth == $b) { $selected = 'selected'; } else { $selected = ''; } echo "<option value=\"$b\" $selected>$b</option>"; $a++; } ?> </select> Try that. Edit: I also recommedn changing $selected = 'selected'; to $selected = 'selected=\"selected\"'; so that it is correct HTML encoding. Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 16, 2007 Share Posted November 16, 2007 Just for fun, I'll throw this one in as another option: <select name="birthmonth"> <?php $m = 'January'; for ($i = 0; $i < 12; $i++) { echo "<option name=\"$m\""; echo ($m == $regbirthmonth) ? ' selected="selected"' : ''; echo ">$m</option>\n"; $m = date('F', strtotime("$m + 1 month")); } ?> </select> Quote Link to comment Share on other sites More sharing options...
scarhand Posted November 16, 2007 Author Share Posted November 16, 2007 thanks alot! i ended up using metrostars' solution i was actually looking into mktime just before i posted this thread too thank you obsidian for yours as well cheers Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.