loquela Posted August 11, 2009 Share Posted August 11, 2009 Hi Guys As you can see the code below displays a dropdwon datepicker. For some reason that I can't figurre out the selected attribute on the MONTH field isn't working althou if I print $nowmonth I get the correct value. DAY and YEAR work fine. can anyone see the problem? Cheers <?php $nowday = date("d"); $nowmonth = date("m"); $nowyear = date("Y"); ?> <select name="arriveday" > //************* FOLLOWING SELECT ATTRIBUTE GETS INSERTED CORRECTLY ********** <option value=01 <?php if ($nowday == 01) { echo"selected='selected'"; } ?>>01</option> <option value=02 <?php if ($nowday == 02) { echo"selected='selected'"; } ?>>02</option> <option value=03 <?php if ($nowday == 03) { echo"selected='selected'"; } ?>>03</option> <option value=04 <?php if ($nowday == 04) { echo"selected='selected'"; } ?>>04</option> <option value=05 <?php if ($nowday == 05) { echo"selected='selected'"; } ?>>05</option> <option value=06 <?php if ($nowday == 07) { echo"selected='selected'"; } ?>>06</option> <option value=07 <?php if ($nowday == 07) { echo"selected='selected'"; } ?>>07</option> <option value=08 <?php if ($nowday == 08) { echo"selected='selected'"; } ?>>08</option> <option value=09 <?php if ($nowday == 09) { echo"selected='selected'"; } ?>>09</option> <option value=10 <?php if ($nowday == 10) { echo"selected='selected'"; } ?>>10</option> <option value=11 <?php if ($nowday == 11) { echo"selected='selected'"; } ?>>11</option> <option value=12 <?php if ($nowday == 12) { echo"selected='selected'"; } ?>>12</option> <option value=13 <?php if ($nowday == 13) { echo"selected='selected'"; } ?>>13</option> <option value=14 <?php if ($nowday == 14) { echo"selected='selected'"; } ?>>14</option> <option value=15 <?php if ($nowday == 15) { echo"selected='selected'"; } ?>>15</option> <option value=16 <?php if ($nowday == 16) { echo"selected='selected'"; } ?>>16</option> <option value=17 <?php if ($nowday == 17) { echo"selected='selected'"; } ?>>17</option> <option value=18 <?php if ($nowday == 18) { echo"selected='selected'"; } ?>>18</option> <option value=19 <?php if ($nowday == 19) { echo"selected='selected'"; } ?>>19</option> <option value=20 <?php if ($nowday == 20) { echo"selected='selected'"; } ?>>20</option> <option value=21 <?php if ($nowday == 21) { echo"selected='selected'"; } ?>>21</option> <option value=22 <?php if ($nowday == 22) { echo"selected='selected'"; } ?>>22</option> <option value=23 <?php if ($nowday == 23) { echo"selected='selected'"; } ?>>23</option> <option value=24 <?php if ($nowday == 24) { echo"selected='selected'"; } ?>>24</option> <option value=25 <?php if ($nowday == 25) { echo"selected='selected'"; } ?>>25</option> <option value=26 <?php if ($nowday == 26) { echo"selected='selected'"; } ?>>26</option> <option value=27 <?php if ($nowday == 27) { echo"selected='selected'"; } ?>>27</option> <option value=28 <?php if ($nowday == 28) { echo"selected='selected'"; } ?>>28</option> <option value=29 <?php if ($nowday == 29) { echo"selected='selected'"; } ?>>29</option> <option value=30 <?php if ($nowday == 30) { echo"selected='selected'"; } ?>>30</option> <option value=31 <?php if ($nowday == 31) { echo"selected='selected'"; } ?>>31</option> </select> //************* FOLLOWING SELECT ATTRIBUTE DOESN'T GET INSERTED ********** <select name="arrivemonth"> <option value=01 <?php if ($nowmonth == 01) { echo"selected='selected'"; } ?>>January</option> <option value=02 <?php if ($nowmonth == 02) { echo"selected='selected'"; } ?>>February</option> <option value=03 <?php if ($nowmonth == 03) { echo"selected='selected'"; } ?>>March</option> <option value=04 <?php if ($nowmonth == 04) { echo"selected='selected'"; } ?>>April</option> <option value=05 <?php if ($nowmonth == 05) { echo"selected='selected'"; } ?>>May</option> <option value=06 <?php if ($nowmonth == 06) { echo"selected='selected'"; } ?>>June</option> <option value=07 <?php if ($nowmonth == 07) { echo"selected='selected'"; } ?>>July</option> <option value=08 <?php if ($nowmonth == 08) { echo"selected='selected'"; } ?>>August</option> <option value=09 <?php if ($nowmonth == 09) { echo"selected='selected'"; } ?>>September</option> <option value=10 <?php if ($nowmonth == 10) { echo"selected='selected'"; } ?>>October</option> <option value=11 <?php if ($nowmonth == 11) { echo"selected='selected'"; } ?>>November</option> <option value=12 <?php if ($nowmonth == 12) { echo"selected='selected'"; } ?>>December</option> </select> //************* FOLLOWING SELECT ATTRIBUTE GETS INSERTED CORRECTLY ********** <select name="arriveyear"> <option value=2009 <?php if ($nowyear == 2009) { echo"selected='selected'"; } ?>>2009</option> <option value=2010 <?php if ($nowyear == 2010) { echo"selected='selected'"; } ?>>2010</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/169745-solved-date-dropdown-selected-issue/ Share on other sites More sharing options...
JonnoTheDev Posted August 11, 2009 Share Posted August 11, 2009 You can easily cut out all that unnecessary duplicate code with a loop. <select name="arriveday"> <?php for($x = 1; $x <= 31; $x++) { $dayNum = str_pad($x, 2, "0", STR_PAD_LEFT); print "<option value='".$dayNum."'".((date("d") == $dayNum) ? " selected" : false).">".$dayNum."</option>\n"; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/169745-solved-date-dropdown-selected-issue/#findComment-895500 Share on other sites More sharing options...
loquela Posted August 11, 2009 Author Share Posted August 11, 2009 Hi thanks for that. But any ideas why the select attribute on the month field is not working? Quote Link to comment https://forums.phpfreaks.com/topic/169745-solved-date-dropdown-selected-issue/#findComment-895503 Share on other sites More sharing options...
JonnoTheDev Posted August 11, 2009 Share Posted August 11, 2009 Because your HTML is bad. Enclose values in quotes. Also 01,02, etc is not an integer in php. <option value=01 <?php if ($nowday == 01) { echo"selected='selected'"; } ?>>01</option> Corrected <option value="01" <?php if ($nowday == "01") { echo "selected"; } ?>>01</option> Please use a loop. Writing it out like that looks awful. Quote Link to comment https://forums.phpfreaks.com/topic/169745-solved-date-dropdown-selected-issue/#findComment-895507 Share on other sites More sharing options...
avvllvva Posted August 11, 2009 Share Posted August 11, 2009 Check the IF condition, Put 08 in quotes, bcoz date function returns string, so you have to change the comparison with quotes from the whole script. $nowmonth == '08' Quote Link to comment https://forums.phpfreaks.com/topic/169745-solved-date-dropdown-selected-issue/#findComment-895510 Share on other sites More sharing options...
loquela Posted August 11, 2009 Author Share Posted August 11, 2009 Thanks avvllvva, you had the answer. I was confused though 'cos the day and the year work fine... Quote Link to comment https://forums.phpfreaks.com/topic/169745-solved-date-dropdown-selected-issue/#findComment-895516 Share on other sites More sharing options...
JonnoTheDev Posted August 11, 2009 Share Posted August 11, 2009 Thanks avvllvva, you had the answer Quote Link to comment https://forums.phpfreaks.com/topic/169745-solved-date-dropdown-selected-issue/#findComment-895517 Share on other sites More sharing options...
thebadbad Posted August 11, 2009 Share Posted August 11, 2009 As Neil correctly pointed out, an integer with a leading zero is treated as an octal, hence your problems. Also, 09 isn't a valid octal. Quote Link to comment https://forums.phpfreaks.com/topic/169745-solved-date-dropdown-selected-issue/#findComment-895521 Share on other sites More sharing options...
avvllvva Posted August 11, 2009 Share Posted August 11, 2009 yes guys... here we are actually comparing strings... not integers.. not octals Quote Link to comment https://forums.phpfreaks.com/topic/169745-solved-date-dropdown-selected-issue/#findComment-895523 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.