Forum Moderators: phranque

Message Too Old, No Replies

coding problem

summarise the coding

         

charonlee

5:35 am on Mar 14, 2003 (gmt 0)

10+ Year Member



hi,

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

%>

jatar_k

5:55 am on Mar 14, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you thought about using a switch instead of all the else if's?

<added>Where are my manners, Welcome to WebmasterWorld charonlee

charonlee

6:06 am on Mar 14, 2003 (gmt 0)

10+ Year Member



Swicth? never use it, is there any "resource" you can recommend? So that I can refer.
TQ

jatar_k

6:28 am on Mar 14, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well my experience lies in php (and a php resource wouldn't help) but it looks like this

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.

Allen

1:29 pm on Mar 14, 2003 (gmt 0)

10+ Year Member



[devedge.netscape.com...]

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]

charonlee

1:32 pm on Mar 14, 2003 (gmt 0)

10+ Year Member



Thanks for all the reply, i have alreday get a better solution on this problem.:))