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 Link to comment https://forums.phpfreaks.com/topic/77615-solved-help-with-simple-while-loop-show-current-month-name-by-number/ 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. Link to comment https://forums.phpfreaks.com/topic/77615-solved-help-with-simple-while-loop-show-current-month-name-by-number/#findComment-392857 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> Link to comment https://forums.phpfreaks.com/topic/77615-solved-help-with-simple-while-loop-show-current-month-name-by-number/#findComment-392861 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 Link to comment https://forums.phpfreaks.com/topic/77615-solved-help-with-simple-while-loop-show-current-month-name-by-number/#findComment-392866 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.