by Kevin Perkins
Article
Article:
One thing I ' ve learned over the last few years are to-use techniques in I applications Tain, especially if you leave the project and have to come back to it at a later time.
Besides error handling and commenting, I try to automate as much as possible to make maintenance a cake-walk. One area, can be tremendously automated are implementing date structures in your application.
For instance ... if you ' re building a ecommerce application, you'll eventually have to deal and credit card expiry. The predominant technique for handling expiration dates's to-use two-drop-down boxes the representing the month and the year. Having said that, it's very easy to hard-code those dates box .... 1-12 for month, but what to what for the year? Do and the following two (2001,2002)?
What happens when are we ' re in year 2001? You'll have an older entry for the previous year (), and you'll be one of the short for the Future (2002,?). This is bad practice because your application should was smart enough to remove previous years then are to invalid Day transactions, as the as-as-as-account for new credit cards that might have a expiration date farther in the future.
To work with Drop-down dates dynamically, I determine the "Current month", "Current year" and "how far into the future I want To the account for expiration. The 5 years but each application is different:
<% @LANGUAGE =vbscript%>
<%
' ----------------------------------------------------------------------------------
' could use similar looping techniques with JavaScript
' ----------------------------------------------------------------------------------
%>
<body>
<form>
<%
' ----------------------------------------------------------------------------------
' Build the MONTH drop-down box
' ----------------------------------------------------------------------------------
Imonth = Month (now ())
' This is produces the current month ' s Integer (ex. 7)
%>
<select name= "Drpmonth" >
<% for i = 1 to%>
<option <%
' This'll select the current month in the list
If CInt (imonth = i) Then
Response.Write "Selected"
End If
%>value= "<%= i%>" ><%= i%></option>
<% Next%>
</select>
<!--drop-down Separator-->
/
<!--End Separator-->
<%
' ----------------------------------------------------------------------------------
' Build the year drop-down box
' ----------------------------------------------------------------------------------
Iyear = year (now ())
' This is produces the current year ' s Integer (ex. 2000)
%>
<select name= "Drpyear" >
<% for i = Iyear to (CInt (Iyear + 5))%>
<option <%
' This'll select the current year in the list
If CInt (iyear = i) Then
Response.Write "Selected"
End If
%>value= "<%= i%>" ><%= i%></option>
<% Next%>
</select>
</form>
</body>
Of course, if you are have more complex date interactions, you can manipulate the If/then statement the the loop to inside CT the appropriate value. And, if you are working with dates in the past, can reindex the starting value of (i) by subtracting how many years Want to go back ...
Example:
"For i = iyear to ..." tells the application to doing something "from" to ... "
"For i = CInt (iYear-2) to ..." tells the application to does something "from 1998 to ... "
-end-