...

View Full Version : month year drop down



Kal
05-08-2007, 01:03 PM
Hi Guys

The following code originally posted by raf works great, howeveri need some help modifying it slightly.

is there away in which it could display all 12 months for the current year? but the default should remain on the current month.

at the moment it only displays the upcoming 5 months. and not the previous months already gone in the year.

thanks in advance




<select name="month">
<%
Dim i2, value, optionv
i2 = 0
do while i2 <= 5
value = Month(DateAdd("m",i2, Now()))
optionv = MonthName(value) + " " + Right(Year(Now),2)
response.write("<option value='" & optionv & "'>"& optionv &"</option>")
i2= i2+1
loop
%>
</select>

TheShaner
05-08-2007, 02:15 PM
<select name="month">
<%
Dim i, cur_month, optionv, sel_text
sel_text = "" 'Display whether the month is selected
cur_month= Month(Now) 'Current Month's integer
For i = 1 To 12
If (i = cur_month) Then sel_text = " selected='selected'"
optionv = MonthName(i) & " " & Right(Year(Now), 2)
Response.Write("<option value='" & optionv & "'" & sel_text & ">" & optionv & "</option>")
Next
%>
</select>
The above code is just looping through all 12 months and displaying them. If during the loop it comes to a month that is equal to the current month's integer, then it'll display the text to make sure that month is selected by default in the drop down.

-Shane

Kal
05-08-2007, 04:37 PM
thanks for your reply just tested your code and it seems to default to December 07 rather than May 07.

TheShaner
05-08-2007, 04:52 PM
My fault! What was happening was that once it got to May, although it set the text to display the selected='selected' in the May 07 option, it never reset it back to nothing and so every month after it also received selected='selected'. If you look at the source code, you'll see. I tested the below code and it works. The red text indicates the changed code.

<select name="month">
<%
Dim i, cur_month, optionv, sel_text
sel_text = "" 'Display whether the month is selected
cur_month= Month(Now) 'Current Month's integer
For i = 1 To 12
If (i = cur_month) Then
sel_text = " selected='selected'"
Else
sel_text = ""
End If
optionv = MonthName(i) & " " & Right(Year(Now), 2)
Response.Write("<option value='" & optionv & "'" & sel_text & ">" & optionv & "</option>")
Next
%>
</select>
-Shane

Kal
05-08-2007, 04:55 PM
thanks shane works great. i looked at the source code and see what you mean.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum