Forum Moderators: phranque
I feel that the code is too long, is there any other way to make the code shorter, like creating function?
The only function that I can think of is creating a function for passing the SQL statement.
Below is the code.
<%Dim i, fday, fmon, fyear, mySql, fday1, fmon1, fyear1, fromDate, toDate
i = 1
fday1 = Request.Form("dday1")
fmon1 = Request.Form("dmon1")
fyear1 = Request.Form("dyear1")
fday = Request.Form("dday")
fmon = Request.Form("dmon")
fyear = Request.Form("dyear")
If Request.Form("project") = "All" Then
if fday <> "" and fmon <> "" and fday1 <> "" and fmon1 <> "" Then
fromDate = DateSerial(fyear1, fmon1, fday1)
toDate = DateSerial( fyear, fmon, fday)
mySql = "SELECT * FROM futurelaunches WHERE Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
elseif fday = "" And fmon <> "" and fday1 <> "" and fmon1 <> "" Then
fromDate = DateSerial(fyear1, fmon1, fday1)
'give 0 as the day, then Date Serial will figure out you want the day before the first day of the given month, which is the last day of the prior month
toDate = DateSerial(fyear, fmon + 1, 0)
mySql = "SELECT * FROM futurelaunches WHERE Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
elseif fday <> "" and fmon <> "" and fday1 = "" and fmon1 <> "" Then
'day 1 of the Month in the year
fromDate = DateSerial(fyear1, fmon1, 1)
toDate = DateSerial(fyear, fmon, fday)
mySql = "SELECT * FROM futurelaunches WHERE Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
elseif fday1 = "" and fday = "" and fmon <> "" and fmon1 <> "" Then
fromDate = DateSerial(fyear1, fmon1, 1)
toDate = DateSerial(fyear, fmon+1, 0)
mySql = "SELECT * FROM futurelaunches WHERE Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
elseif fday1 = "" and fmon1 = "" and fday <> "" and fmon <> "" Then
fromDate = DateSerial(fyear1, 1, 1)
toDate = DateSerial(fyear, fmon, fday)
mySql = "SELECT * FROM futurelaunches WHERE Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
elseif fday1 <> "" and fmon1 <> "" and fday = "" and fmon = "" Then
fromDate = DateSerial(fyear1, fmon1, fday1)
toDate = DateSerial(fyear+1, 1, 0)
mySql = "SELECT * FROM futurelaunches WHERE Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
elseif fday1 = "" and fmon1 = "" and fday = "" and fmon <> "" Then
fromDate = DateSerial(fyear1, 1, 1)
toDate = DateSerial(fyear+1, fmon+1, 0)
mySql = "SELECT * FROM futurelaunches WHERE Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
elseif fday1 = "" and fmon1 <> "" and fday = "" and fmon = "" Then
fromDate = DateSerial(fyear1, fmon1, 1)
toDate = DateSerial(fyear+1, 1, 0)
mySql = "SELECT * FROM futurelaunches WHERE Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
else
fromDate = DateSerial(fyear1, 1, 1)
toDate = DateSerial(fyear+1, 1, 0)
Response.Write fromDate & "br"
Response.Write toDate
mySql = "SELECT * FROM futurelaunches WHERE Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
End If
Else
if fday <> "" and fmon <> "" and fday1 <> "" and fmon1 <> "" Then
fromDate = DateSerial(fyear1, fmon1, fday1)
toDate = DateSerial( fyear, fmon, fday)
mySql = "SELECT * FROM futurelaunches WHERE Project = '" & TRIM(Request.Form("project")) & "' And Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
elseif fday = "" And fmon <> "" and fday1 <> "" and fmon1 <> "" Then
fromDate = DateSerial(fyear1, fmon1, fday1)
'give 0 as the day, then Date Serial will figure out you want the day before the first day of the given month, which is the last day of the prior month
toDate = DateSerial(fyear, fmon + 1, 0)
mySql = "SELECT * FROM futurelaunches WHERE Project = '" & TRIM(Request.Form("project")) & "' And Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
elseif fday <> "" and fmon <> "" and fday1 = "" and fmon1 <> "" Then
'day 1 of the Month in the year
fromDate = DateSerial(fyear1, fmon1, 1)
toDate = DateSerial(fyear, fmon, fday)
mySql = "SELECT * FROM futurelaunches WHERE Project = '" & TRIM(Request.Form("project")) & "' And Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
elseif fday1 = "" and fday = "" and fmon <> "" and fmon1 <> "" Then
fromDate = DateSerial(fyear1, fmon1, 1)
toDate = DateSerial(fyear, fmon+1, 0)
mySql = "SELECT * FROM futurelaunches WHERE Project = '" & TRIM(Request.Form("project")) & "' And Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
elseif fday1 = "" and fmon1 = "" and fday <> "" and fmon <> "" Then
fromDate = DateSerial(fyear1, 1, 1)
toDate = DateSerial(fyear, fmon, fday)
mySql = "SELECT * FROM futurelaunches WHERE Project = '" & TRIM(Request.Form("project")) & "' And Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
elseif fday1 <> "" and fmon1 <> "" and fday = "" and fmon = "" Then
fromDate = DateSerial(fyear1, fmon1, fday1)
toDate = DateSerial(fyear+1, 1, 0)
mySql = "SELECT * FROM futurelaunches WHERE Project = '" & TRIM(Request.Form("project")) & "' And Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
elseif fday1 = "" and fmon1 = "" and fday = "" and fmon <> "" Then
fromDate = DateSerial(fyear1, 1, 1)
toDate = DateSerial(fyear+1, fmon+1, 0)
mySql = "SELECT * FROM futurelaunches WHERE Project = '" & TRIM(Request.Form("project")) & "' And Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
elseif fday1 = "" and fmon1 <> "" and fday = "" and fmon = "" Then
fromDate = DateSerial(fyear1, fmon1, 1)
toDate = DateSerial(fyear+1, 1, 0)
mySql = "SELECT * FROM futurelaunches WHERE Project = '" & TRIM(Request.Form("project")) & "' And Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
else
fromDate =DateSerial(fyear1,1, 1)
toDate = DateSerial(fyear+1, 0, 1)
mySql = "SELECT * FROM futurelaunches WHERE Project = '" & TRIM(Request.Form("project")) & "' And Date BETWEEN #" & fromDate & "# AND #" & toDate &"# ORDER BY L_Date"
End If
End If
%>
switch ($variable) {
case "1":
do stuff;
break;
case "2":
do stuff;
break;
case "3":
do stuff;
break;
case default:
do stuff;
break;
}
The cases here are very simple but they can be made to accomodate your if cases. You could maybe try a loop of some kind. That is a whole mess of code.
Also, a lot of your SQL statements appear to be exactly the same
Also of note, I'm not sure about ASP, but in PHP you can do:
switch ($variable) {
case "one":
case "two":
case "four":
// This code is executed in cases one, two and four
break;
case "three":
// This code is executed if $variable == "three"
break;
}
[edited by: Allen at 1:33 pm (utc) on Mar. 14, 2003]