1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Easier way to code this?

Discussion in 'C#' started by sethyr, Jan 30, 2010.

  1. #1
    I've created my first asp script to write whether the day is either an A day or a B day.

    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
    
    <%
    dim setday
    dim day
    setday=date()
    
    If setday="1/28/2010" Then
    day="A Day"
    
    ElseIf setday="1/29/2010" Then
    day="B Day"
    
    ElseIf setday="2/1/2010" Then
    day="A Day"
    
    ElseIf setday="2/2/2010" Then
    day="B Day"
    
    Else
    day=""
    
    End If
    
    Dim wordd
    If day="A Day" or day="B Day" Then
    wordd="Today is an"
    Else
    wordd="No School Today"
    End If
    %>
    <% response.write(wordd) %> <strong><% response.write(day) %></strong>.
    
    PHP:

    Is there an easier way to list the days as an A or B day? Weekends + School Holidays would have to be skipped.
    SEMrush
     
    Last edited: Jan 30, 2010
    sethyr, Jan 30, 2010 IP
    SEMrush
  2. hansab

    hansab Active Member

    Messages:
    162
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #2
    I don't have better idea about this but just want to say one thing, that this doesnt look like a first script. Hats off to you. I dont know if others will agree with me, but atleast you are doing good as compare to me when i was newbie :)
     
    hansab, Jan 30, 2010 IP
  3. sethyr

    sethyr Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well I know PHP, so learning what asp code does the equivalent in asp was kind of easy, but so far this is all i can understand about asp. asp is so hard. lol
     
    sethyr, Jan 30, 2010 IP
  4. hansab

    hansab Active Member

    Messages:
    162
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #4
    No, ASP is much easier and close to human language php looks hard for me. But i think we both are wrong lol because you have experience in php so u think asp is hard and same is the case with me with php.
     
    hansab, Jan 31, 2010 IP
  5. RonBrown

    RonBrown Well-Known Member

    Messages:
    934
    Likes Received:
    55
    Best Answers:
    4
    Trophy Points:
    105
    #5
    There's a couple of things you could do to make it a little neater.

    1. There's no need for "A" or "B" days as they are effectively the same thing for the purposes of what you are doing.

    2. Select Case may be easier than If/Else/End if if you plan to have a long list of dates. Also, escape the lists once a match has been found to stop any more processing and save time.

    3. Since most days WILL be a school day, then declare a variable saying the the day IS a school day e.g. Dim DayIsSchoolday = 1, unless it isn't

    4. Check for weekend days first as that will be the quickest way to determine whether it is a school day or not. Do this by creating another variable for what day of the week it is. If it's a Saturday or Sunday, then it is NOT a school day an DayIsSchoolday = 0

    5. If it isn't a weekend day, then check whether it is a school holiday date.

    The processing should now be much quicker and simpler to update.

    Even better, if there is a long string of school holiday dates, set the dates up as a comma-delimited string, then just parse the list - it will make it easier to keep the list updated rather than write a long select statement every time.

    so...

    
    
    <%
    
    Dim DayofWeek
    Dim DayIsSchoolday
    Dim SetDay
    Dim Day
    
    DayIsSchoolday = 1
    DayofWeek = Weekday(date)
    SetDay=date()
    
    
    
    ' check whether it is a weekend day or not. Quick and easy
    
    Select Case DayofWeek
    	Case 1 or 7 ' Check if it is a Saturday or Sunday
    		DayIsSchoolday = 0
    
    End Select
    
    ' If DayisSchoolday is still 1 (it isn't a weekend day), then we check whether it is a school holiday date
    
    If DayIsSchoolday = 1 then
    	Select Case SetDay
    		Case "1/28/2010"
    			DayisSchoolday = 0
    		Case "1/29/2010"
    			DayisSchoolday = 0
    	End Select
    End if
    
    
    Dim wordd
    If DayIsSchoolday=0 then
    	wordd="Today is an"
    Else
    	wordd="No School Today"
    End If
    
    
    response.write(wordd) %> <strong><% response.write(day) %></strong>.
    
    %>
    
    Code (markup):
    Note: I haven't checked this worked, so there may be some errors. If there is you should be able to figure them out.
     
    RonBrown, Jan 31, 2010 IP
  6. Shailendra Mishra

    Shailendra Mishra Peon

    Messages:
    446
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you can replace if elseif structure with cases. nothing much.. this code is quite simple
     
    Shailendra Mishra, Jan 31, 2010 IP
  7. sethyr

    sethyr Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The A and B day is how our scheduling works. Each day alternates classes, so I was trying to get output what day it is. For example this is how the next 2 weeks would go:
    M- A
    T- B
    W- A
    T- B
    F- A
    M- B
    T- A
    W- B
    T- A
    F- B

    When there's holidays the schedule skips that holiday like it does with the weekends.
     
    sethyr, Jan 31, 2010 IP
  8. RonBrown

    RonBrown Well-Known Member

    Messages:
    934
    Likes Received:
    55
    Best Answers:
    4
    Trophy Points:
    105
    #8
    OK, that makes it a bit more difficult, but not impossible to automate.

    You can still use the first part of the code to determine whether it was a weekend day or not.

    The difficult part will be trying to figure out whether it was an A or B day.

    Here's some thoughts.

    1. You will need a reference date to start everything off e.g. Monday 1 February is an "A" day.

    2. The cycle repeats every 14 days starting on a Monday. That means that if you select a date, and that date isn't a Monday, then you need to find out WHAT date the Monday of that week is.

    3. If the number of days between the reference day and the Monday of the week of the date you are testing can be divided by 14 precisely, then you know that Monday will be an "A" date. If the number of days between the reference date and the Monday you have calculated cannot be divided by 14 exactly, then that Monday will be a "B" day. We're forgetting about how holidays affect this at the moment.

    You can use "Mod" to determine this e.g "DaysBetweenMondays mod 14" should equal 0.



    4. Now that you know what type of day the Monday of the selected week will be, you can calculate what type of day the selected date would be by calculating how many days the selected date is from the Monday.

    Again, the mod function would be handy.

    If [number of days from Monday] mod 2 = 0 then it will be an the same type of day as the Monday.

    If [number of days from Monday] mod 2 = 1 then it will be a different type of day as the Monday of that week.

    You are now able to calculate the type of day any selected date (that isn't a weekend day) is.


    However, you now have the problem that school holidays interfere with this nice calculation.

    5. Calculate how many school holiday dates have fallen between the reference point date and the date you are calculating.

    When you have this number - which should be easy to calculate - you can then use Mod (again!!) to calculate the actual type of day.

    If [number of holiday days between reference date and selected date] mod 2 = 0 then the day type is the same as that already calculated.

    If [number of holiday days between reference date and selected date] mod 2 = 1 then it is a different day type that than already calculated.



    You now have the ability to detect whether the date selected is a weekend date. If it isn't a weeked day you will be able to tell whether the selected day would be an A or B day (assuming no holidays), and then alter the result to tell whether previous school holidays will interfere with the type of day is should have been.

    The actual code won't be that hard as ASP has strong date support (datediff()) for instance. It's just thinking through the logic that is difficult.

    Best of luck with this.
     
    RonBrown, Jan 31, 2010 IP
  9. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #9
    I'm not sure if this is completely correct, and you would have to work out the school holidays yourself.. but here is some code to start with:
    
    <%
    Dim setDay, startDate, diff, tmp, day, msg
    
    setDay = Date()
    startDate = "1/25/2010"
    day = "B"
    
    For i = 1 to 5
        diff = DateDiff("d",setDay,startDate)
        tmp = diff MOD 7
        If tmp = 0 AND tmp MOD 2 = 0 Then
            msg = "Today is an " & day & " day."
        ElseIf tmp = 0 AND tmp MOD 2 <> 0 Then
            switch()
            msg = "Today is an " & day & " day."
        End IF
        switch()
        startDate = DateAdd("d",1,startDate)
    Next
    
    If msg = "" Then msg = "No School Today"
    Response.Write(msg)
    
    Sub switch()
        If day = "A" Then 
           day = "B"
        Else
           day = "A"
        End IF
    End Sub
    
    %>
    
    Code (markup):
    basically the idea is to check if the number of days from the current date to the beginning date is divisible by 7.. if it is, then we check if it is an even number (same type of day as the corresponding date, "A"), or an odd number (opposite type of day, "B"). We do this for all five days of which only one will match... if there is no match then it is a weekend.
     
    Last edited: Feb 1, 2010
    camjohnson95, Feb 1, 2010 IP
  10. sethyr

    sethyr Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks everyone :) I got it working.
     
    sethyr, Feb 4, 2010 IP