McMaster Posted May 24, 2010 Share Posted May 24, 2010 I have a script that is supposed to show today's current date in drop down menu's but when I try it on my server I am given the date, 31st December 2010. Does anybody know what I am doing wrong here? <?php $months = array ('January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December'); $weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); $days = range (1, 31); $years = range (2010, 2020); $currentDay = date('d'); $currentMonth = date('F'); $currentYear = date('Y'); echo "<select name='weekday'>"; foreach ($days as $value) { if($value == $currentDay){ $default = 'selected="selected"'; } echo '<option '.$default.' value="'.$value.'">'.$value.'</option>\n'; } echo '</select> '; echo "<select name='month'>"; foreach ($months as $value) { if($value == $currentMonth){ $default = 'selected="selected"';} echo '<option '.$default.' value="'.$value.'">'.$value.'</option>\n'; } echo '</select> '; echo "<select name='year'>"; foreach ($years as $value) { if($value == $currentYear){ $default = 'selected="selected"';} echo '<option '.$default.' value="'.$value.'">'.$value.'</option>\n'; } echo '</select> '; ?> Thanks for your help guys Link to comment https://forums.phpfreaks.com/topic/202733-php-dropdown-with-current-date-set-help-with-my-code/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 24, 2010 Share Posted May 24, 2010 Did you do a 'view source' of the resulting HTML to see what you are getting? Link to comment https://forums.phpfreaks.com/topic/202733-php-dropdown-with-current-date-set-help-with-my-code/#findComment-1062583 Share on other sites More sharing options...
kenrbnsn Posted May 24, 2010 Share Posted May 24, 2010 The reason this is happening is that once you set the $default string, you never reset it. You would have seen this if you had looked at the generated source. This code works: <?php $months = array ('January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December'); $weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); $days = range (1, 31); $years = range (2010, 2020); $currentDay = date('j'); $currentMonth = date('F'); $currentYear = date('Y'); echo "<select name='weekday'>"; foreach ($days as $value) { $default = ($value == $currentDay)?'selected="selected"':''; echo '<option '.$default.' value="'.$value.'">'.$value."</option>\n"; } echo '</select> '; echo "<select name='month'>\n"; foreach ($months as $value) { $default = ($value == $currentMonth)?'selected="selected"':''; echo '<option '.$default.' value="'.$value.'">'.$value."</option>\n"; } echo '</select> '; echo "<select name='year'>"; foreach ($years as $value) { $default = ($value == $currentYear)?'selected="selected"':''; echo '<option '.$default.' value="'.$value.'">'.$value."</option>\n"; } echo '</select> '; ?> Ken Link to comment https://forums.phpfreaks.com/topic/202733-php-dropdown-with-current-date-set-help-with-my-code/#findComment-1062584 Share on other sites More sharing options...
McMaster Posted May 24, 2010 Author Share Posted May 24, 2010 Ah yes, I viewed the source and saw that every option had the select set. You're code worked great Ken, cheers. Link to comment https://forums.phpfreaks.com/topic/202733-php-dropdown-with-current-date-set-help-with-my-code/#findComment-1062586 Share on other sites More sharing options...
katierosy Posted May 25, 2010 Share Posted May 25, 2010 Some times when "else" is ignored in "if" block, it becomes difficult to find the error in the code quickly.Because it looks to be 100 % correct. But the script wont work. <?php $months = array ('January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November','December'); $weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); $days = range(1,31); $years = range (2010, 2020); $currentDay = date('d'); $currentMonth = date('F'); $currentYear = date('Y'); echo "<select name='weekday'>"; foreach($days as $valued) { if($valued == $currentDay) { $default = 'selected="selected"'; echo '<option '.$default.' value="'.$valued.'">'.$valued.'</option>\n'; } else { $default=''; echo '<option '.$default.' value="'.$valued.'">'.$valued.'</option>\n'; } } echo '</select> '; echo "<select name='month'>"; foreach($months as $valuem) { if($valuem==$currentMonth) { $default1 = 'selected="selected"'; echo '<option '.$default1.' value="'.$valuem.'">'.$valuem.'</option>\n'; } else { $default1 = ''; echo '<option '.$default1.' value="'.$valuem.'">'.$valuem.'</option>\n'; } } echo '</select> '; echo "<select name='year'>"; foreach ($years as $valuey) { if($valuey == $currentYear) { $default2 = 'selected="selected"'; echo '<option '.$default2.' value="'.$valuey.'">'.$valuey.'</option>\n'; } else { $default2 = ''; echo '<option '.$default2.' value="'.$valuey.'">'.$valuey.'</option>\n'; } } echo '</select> '; ?> Link to comment https://forums.phpfreaks.com/topic/202733-php-dropdown-with-current-date-set-help-with-my-code/#findComment-1063056 Share on other sites More sharing options...
kenrbnsn Posted May 25, 2010 Share Posted May 25, 2010 Why did you post on a topic that was marked as "solved" with a solution that's longer and more involved than the posted solution and harder to read since you didn't put your code between tags? Ken Link to comment https://forums.phpfreaks.com/topic/202733-php-dropdown-with-current-date-set-help-with-my-code/#findComment-1063123 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.