tracedef Posted January 29, 2010 Share Posted January 29, 2010 We have two select boxes in a form, one lists months and one lists years. We want use a conditional statement to test to see if each month / year option in each select box is the current month or year and then apply a selected="selected" attribute if it is. This will enable the select boxes to always default to current month / year. I'm not quite sure where to even start in terms of php. We're looking to test if option value is equal to current month, if so, apply selected="selected", otherwise do nothing.... One section of select box: <option label="Jan" value="1" selected="selected">Jan</option> Here is a yearly example: <option label="2010" value="2010" selected="selected">2010</option> One select box currently looks like this: <select name="sm"> <option label="Jan" value="1">Jan</option> <option label="Feb" value="2" [color=red]selected="selected"[/color]>Feb</option> <option label="Mar" value="3">Mar</option> <option label="Apr" value="4">Apr</option> <option label="May" value="5">May</option> <option label="Jun" value="6">Jun</option> <option label="Jul" value="7">Jul</option> <option label="Aug" value="8">Aug</option> <option label="Sep" value="9">Sep</option> <option label="Oct" value="10">Oct</option> <option label="Nov" value="11">Nov</option> <option label="Dec" value="12">Dec</option> </select> Any feedback greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/190288-need-help-with-conditional-to-determine-if-month-listed-is-current-month/ Share on other sites More sharing options...
schilly Posted January 29, 2010 Share Posted January 29, 2010 well first off get the current month and year. $month = date('n'); $year = date('Y'); Then add this to your month select option: <option label="Jan" value="1" <?php echo ($month==1) ? "selected='selected'" : ""; ?>>Jan</option> 1 will change to 2 and etc etc as you go thru the months. year is the exact same as thing. let me know if you have any questions. Link to comment https://forums.phpfreaks.com/topic/190288-need-help-with-conditional-to-determine-if-month-listed-is-current-month/#findComment-1003910 Share on other sites More sharing options...
tracedef Posted January 29, 2010 Author Share Posted January 29, 2010 Great, thank you so much! Link to comment https://forums.phpfreaks.com/topic/190288-need-help-with-conditional-to-determine-if-month-listed-is-current-month/#findComment-1003964 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.