Link to home
Start Free TrialLog in
Avatar of McSqueeb
McSqueeb

asked on

ASP Calendar, a monthly calendar that shows events/dates using an array.

I am developing a calendar that will allow users to see what date(s) my business is closed on, currently my app checks the database every time a new date is written (1 - 31 of a month) this isnt good use of a db connection.

I want to change this so that an array is built before I start printing the calendar therefor the script checks the array and not the database on average 30times for a given month.

I havent found any topics / samples that create an array (not visible to the user) from a access database, then allow a script to search through that array to find a match.
SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of McSqueeb
McSqueeb

ASKER

Here is my current loop, it checks the database as the calendar squares are created.

For iWeek = 1 To iRows
               Response.Write "<tr valign='top' class='PLAIN'>"
          For iDay = 1 To iColumns
      ' Checks to see if there is a day this month on the date being written
                        
      If aCalendarDays((iWeek-1)*7 + iDay) > 0 then
                    dtOnDay = DateSerial(Year(dtCurrentDate), Month(dtCurrentDate), aCalendarDays((iWeek-1)*7 + iDay))

' define what type of event it is
strHol = "Holiday"
strOcc = "Occasion"
' set up database connection & sql
Set objRS = Server.CreateObject("ADODB.RecordSet")
strSQL = "SELECT CalID, CalDateFrom, CalDateTo, CalType FROM tblCalendar WHERE CalDateFrom >= #" & formatdatetime(dtOnDay, vbLongDate) & "#"
Set objRS = objConnNNC.Execute(strSQL)
' DO UNTIL NO RECORDS FOUND
If NOT objRS.EOF THEN
      
      IF dtOnDay = dtToday THEN
      
      IF dtOnDay = objRS("CalDateFrom") AND objRS("CalType") = strHol THEN
                  strTAB =  "'CALENDER_CURRENT_HOL'"      
      ELSEIF dtOnDay = objRS("CalDateFrom") AND objRS("CalType") = strOcc THEN
                  strTAB =  "'CALENDER_CURRENT_OCC'"      
      ELSE
                  strTAB =  "'CALENDER_CURRENT_BLA'"
      END IF
                        
      ELSEIF dtOnDay = objRS("CalDateFrom") AND objRS("CalType") = strHol  THEN
            strTAB =  "'CALENDAR_Holiday'"      
      ELSEIF dtOnDay = objRS("CalDateFrom") AND objRS("CalType") = strOcc THEN
            strTAB =  "'CALENDAR_Occasion'"      
      ELSE
            strTAB =  "'CALENDAR_BLANK'"      
      END IF

ELSE

strTAB =  "'CALENDAR_BLANK'"

END IF
Set objRS = Nothing                        
                  
                        
Response.Write ("<TD CLASS=" & strTAB & ">&nbsp;<span class='CALENDAR_DATE'><a href='calendar.asp?CID=" & dtOnDay & "'>" & aCalendarDays((iWeek-1)*7 + iDay) & "</a></span><BR>&nbsp;")
      Else
        Response.Write ("<TD>&nbsp;")
      End IF
                        
        Response.Write "</TD>"
            Next
            Response.Write "</TR>"
Next


Has anyone created a calendar that displays dates (bank holidays / xmas hols) as a different coloured cell, i have searched the web and every calendar has txt links which im not looking for. Thanks to dwaldner & GaryC so far, im looking at GetRows() as we speak.

I've created a calendar that displays events on a calendar....but I've never put holidays on the calendar...
Ok this is code I use to display a calendar.  Dates with appointments are greyed out.  I havent got time to go through this tonight, but if as I assume you store the dates in the db that you're closed you should be able to tie this in quite easily

<%
      iDate=request("date")
      If IsDate(iDate) Then iDate = CDate(iDate) Else iDate = date
      monthstart=session("db_enclose") & year(idate) & "/" & month(idate) & "/1" & session("db_enclose")
      monthend=dateadd("d",-1,dateadd("m",1,(year(idate) & "/" & month(idate) & "/1")))
      monthend=session("db_enclose") & year(monthend) & "/" & month(monthend) & "/" & day(monthend) & session("db_enclose")
      rs.open "select cal_date from calendar where cal_date between " & monthstart & " AND " & monthend & " ORDER by cal_date",session("conn")
      iDIM = GetDaysInMonth(Month(iDate), Year(iDate))
      iDOW = GetWeekdayMonthStartsOn(iDate) %>
      <table width=98% height=100% cellspacing=0 cellpadding=0>
      <TR><TD><%call bar(Session(191)&" - " & formatdatetime(iDate,vblongdate))%></TD></TR>
      <TR><TD class=xpboxbottom align=center valign=top height=100%>
      <table><TR><TD align="center" valign="top">
      <TABLE BORDER=5 CELLSPACING=0 CELLPADDING=1>
      <TR><TD BGCOLOR=#000099 ALIGN="center" height=1 COLSPAN=7>
      <TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=0 style="border-collapse: collapse" bordercolor="#111111">
      <TR><TD align=right><A HREF="?page=calendar&date=<%= SubtractOneMonth(iDate)%>" style="text-decoration: none"><font color="#FFFFFF"><<</font></A></TD>
      <TH><font color="#FFFFFF"><%=MonthName(Month(iDate)) & " " & Year(iDate)%></font></TD>
      <TD><A HREF="?page=calendar&date=<%=AddOneMonth(iDate)%>" style="text-decoration: none"><font color="#FFFFFF">>></font></A></TD></TR>
      </TABLE></TD></TR><TR><TH>S</TH><TH>M</TH><TH>T</TH><TH>W</TH><TH>T</TH><TH>F</TH><TH>S</TH></TR><%
      If iDOW <> 1 Then
            Response.Write "<TR>"
            iPosition = 1
            Do While iPosition < iDOW
                  Response.Write "<TD BGCOLOR=#99CCFF >&nbsp;</TD>"
                  iPosition = iPosition + 1
            Loop
      End If
      iCurrent = 1
      iPosition = iDOW
      Do While iCurrent <= iDIM
            if not rs.eof then tempdate=year(rs(0)) & "/" & month(rs(0)) & "/" & icurrent else tempdate="2000/1/1"
            if not rs.eof then checkdate=year(rs(0)) & "/" & month(rs(0)) & "/" & day(rs(0)) else checkdate="1900/1/1"
            If iPosition = 1 Then
                  Response.Write "<TR>"
            End If
            If iCurrent = Day(iDate) Then
                  Response.Write "<TD align=center style=""border-width:2;border-color:black"""
                  if tempdate=checkdate then %>
                        style="cursor:hand;cursor:pointer" BGCOLOR=#C0C0C0 onclick='location.href="?page=calendar&date=<%=year(iDate)&"/"&month(iDate)&"/"&iCurrent%>"'><%=iCurrent%></TD><%
                        rs.movenext
                  else %>
                        style="cursor:hand;cursor:pointer" BGCOLOR=#E6F2FF onclick='location.href="?page=calendar&date=<%=year(iDate)&"/"&month(iDate)&"/"&iCurrent%>"'><%=iCurrent%></TD><%
                  end if
            Else
                  if tempdate=checkdate then %>
                        <TD bgcolor=#C0C0C0 style="cursor:hand;cursor:pointer" align=center onclick='location.href="?page=calendar&date=<%=year(iDate)&"/"&month(iDate)&"/"&iCurrent%>"'><%=iCurrent%></TD><%
                        rs.movenext
                  else %>
                        <TD bgcolor=#E6F2FF style="cursor:hand;cursor:pointer" align=center onclick='location.href="?page=calendar&date=<%=year(iDate)&"/"&month(iDate)&"/"&iCurrent%>"'><%=iCurrent%></TD><%
                  end if
            End If
            If iPosition = 7 Then
                  Response.Write "</TR>"
                  iPosition = 0
            End If
            iCurrent = iCurrent + 1
            iPosition = iPosition + 1
      Loop
      rs.close
      If iPosition <> 1 Then
            Do While iPosition <= 7
                  Response.Write "<TD BGCOLOR=#99CCFF>&nbsp;</TD>"
                  iPosition = iPosition + 1
            Loop
            Response.Write "</TR>"
      End If %>
      </TABLE><BR><BR><%
      call button(Session(192),"window.open('caltime.asp?date=" & iDate & "','caltime','width=350,height=340')","add") %>
      </TD><TD valign=top width=100%><%
      sql="select * from calendar where cal_date=" & session("db_enclose") & year(iDate) & "/" & month(iDate) & "/" & day(iDate) & session("db_enclose")
      rs.open sql & " and cal_userid=" & session("userid") & " Order by cal_time",session("conn")
      dim calnotes(24,2)
      bgcolor="#E7E8F9"
      do while not rs.eof
            temptime=rs("cal_time")
            if minute(rs("cal_time"))=00 then minutes=0 else minutes=1
            calnotes(hour(rs("cal_time")),minutes)="<span onclick=""window.open('caltime.asp?cal_id=" & rs("cal_id") & "','caltime','width=350,height=340')"" style='cursor:pointer;cursor:hand;width:100%;background:" & bgcolor & "'>" & rs("cal_subject") & "</span><BR>" & calnotes(hour(rs("cal_time")),minutes)
            rs.movenext
            if not rs.eof then if temptime=rs("cal_time") and bgcolor="#E7E8F9" then bgcolor="#CBCDFA" else bgcolor="#E7E8F9"
      loop
      rs.close %>
      <table width=95% border=5 cellspacing="0" cellpadding=0 border="5"><%
      for count=6 to 22 %>
            <TR><TD width=20 align=right style="border-right:none;border-bottom-color:#666666" valign=top rowspan="2"><font size=3><B><%=count%></B></font>&nbsp;</TD>
            <TD style="border-left:none;border-bottom-color:#666666" valign=top width=1 rowspan=2><font size=1>00</font></TD>
            <TD height=15 valign=top><%if calnotes(count,0)="" then response.write "&nbsp;" else response.write calnotes(count,0)%></TD></TR>
            <TR><TD valign=top style="border-bottom-color:#666666"><%if calnotes(count,1)="" then response.write "&nbsp;" else response.write calnotes(count,1)%></TD></TR><%
      next %>
      </table>
      </TD></TR></TABLE><%
      Function GetDaysInMonth(iMonth, iYear)
            dTemp = DateAdd("d", -1, DateSerial(iYear, iMonth + 1, 1))
            GetDaysInMonth = Day(dTemp)
      End Function
      Function GetWeekdayMonthStartsOn(dAnyDayInTheMonth)
            dTemp = DateAdd("d", -(Day(dAnyDayInTheMonth) - 1), dAnyDayInTheMonth)
            GetWeekdayMonthStartsOn = WeekDay(dTemp)
      End Function
      Function SubtractOneMonth(iDate)
            SubtractOneMonth = DateAdd("m", -1, iDate)
      End Function
      Function AddOneMonth(iDate)
            AddOneMonth = DateAdd("m", 1, iDate)
      End Function
%>
i see, this calendar is to alert users to business closing dates for bank holidays and christmas holidays (ireland)

so instead of event   12/08/2004     BBQ
its more like     Bank Holiday 02/05/2005   Christmas Holidays  24/12/2004 to 02/01/2005
im just having problems geting the script to loop until start & finish date are <> 

I could just input the dates seperatly  24/12/2004 - 25/12/2004 - 26/12/2004 etc but that will make size of database huge.



thanks Gary will look over it now.
Ok, like Gary, I'll try and go through this as well......

First, change the line of code below to your query (its the line that's commented blatantly...).  Your query has to have 2 fields, specifically, for this to run....[date of event] and [primary key].

Second, make sure that the connection object is changed too...

If you have any problems, let me know and I'll walk you through it.

Dan

-----


<!--#include virtual="/connections/connection.asp"-->
<%
if request("givenDate") = "" then
  monthDate = Month(Now()) & "/1/2004"
else
  monthDate = request("givenDate")
end if
monthEndDay = DatePart("d",DateAdd("d",-1,DateAdd("m",1,CDate(monthDate))))
firstDay = DatePart("w",monthDate)  '1 = Sunday, 7 = Saturday
counter = 1
set rs = server.createObject("ADODB.RecordSet")
'-------------------------------------------------CHANGE THIS ------------------------------------
rs.Open "SELECT * FROM [Calendar of Events - Populator] WHERE [date of event] >= '" & monthDate & " 12:00:01 AM' AND [date of event] <= '" & Month(monthDate) & "/" & monthEndDay & "/2004 11:59:59 PM' ORDER BY [date of event]",my_conn,3,3
'------------------------------------------------------------------------------------------------------
%>
<form name="changer" method="POST">
<input type="hidden" name="givenDate" value="<%=monthDate%>">
</form>
<table cellspacing="1" cellpadding="0" width="560">
  <tr>
    <td align="left" style="border : 1px solid black;"><a href="javascript: document.changer.givenDate.value = '<%=DateAdd("m",-1,CDate(monthDate))%>'; document.changer.submit();">&lt;&lt;</a></td>
    <td colspan="5" align="center" style="border : 1px solid black;">
      <%=MonthName(Month(monthDate))%>&nbsp;<%=Year(monthDate)%>
    </td>
    <td align="right" style="border : 1px solid black;"><a href="javascript: document.changer.givenDate.value = '<%=DateAdd("m",1,CDate(monthDate))%>'; document.changer.submit();">&gt;&gt;</a></td>
  </tr>
  <tr>
    <td style="border : 1px solid black;" width="80">
      Sun.
    </td>
    <td style="border : 1px solid black;" width="80">
      Mon.
    </td>
    <td style="border : 1px solid black;" width="80">
      Tue.
    </td>
    <td style="border : 1px solid black;" width="80">
      Wed.
    </td>
    <td style="border : 1px solid black;" width="80">
      Thurs.
    </td>
    <td style="border : 1px solid black;" width="80">
      Fri.
    </td>
    <td style="border : 1px solid black;" width="80">
      Sat.
    </td>
  </tr>
  <tr>
  <%
  for i=2 to firstDay
  %>
  <td>&nbsp;</td>
  <%
  next
  %>
  <%
  for i=firstDay to 7
  %>
  <td style="border : 1px solid black;" onMouseOver="this.style.backgroundColor = '#AAAAAA';" onMouseOut="this.style.backgroundColor = '#FFFFFF';" id="day_<%=counter%>"><%=counter%>
  <%
  if not rs.eof then
    Do      
      if rs.eof then
        Exit Do
      elseif DatePart("d",rs("date of event")) <> counter then
        Exit Do
      end if
      %>
     <a href="#<%=rs("primary key")%>"><img src="/common/images/calendar_item_online.gif" border="0"></a>                
      <script language="javascript">
      document.getElementById("day_<%=counter%>").className = "lightBlueCellSmall";
      document.getElementById("day_<%=counter%>").onmouseover = null;
      document.getElementById("day_<%=counter%>").onmouseout = null;
      </script>
      <%
      rs.moveNext
    Loop
  end if
  %>
 
  <BR><BR><BR></td>
  <%
    counter = counter + 1
  next
  weekDayCounter = 1
  %>
  </tr>
  <tr>
  <%
  while counter <= monthEndDay
    if weekDayCounter > 7 then
    %>
  </tr>
  <tr>
    <%
      weekDayCounter = 1
    end if
    %>
    <td style="border : 1px solid black;" onMouseOver="this.style.backgroundColor = '#AAAAAA';" onMouseOut="this.style.backgroundColor = '#FFFFFF';" id="day_<%=counter%>"><%=counter%>
    <%
  if not rs.eof then
    Do      
      if rs.eof then
        Exit Do
      elseif DatePart("d",rs("date of event")) <> counter then
        Exit Do
      end if

      %>
      <a href="#<%=rs("primary key")%>"><img src="/common/images/calendar_item_online.gif" border="0"></a>            
      <script language="javascript">
      document.getElementById("day_<%=counter%>").className = "lightBlueCellSmall";
      document.getElementById("day_<%=counter%>").onmouseover = null;
      document.getElementById("day_<%=counter%>").onmouseout = null;
      </script>
      <%
      rs.moveNext
    Loop
  end if
  %>
    <BR><BR><BR></td>
  <%
    weekDayCounter = weekDayCounter + 1
    counter = counter + 1
  wend
  for i=weekDayCounter to 7
  %>
    <td>&nbsp;</td>
  <%
  next
  %>
  </tr>
</table>
<%
rs.Close
set rs = Nothing
%>  
Just to add there's lots of code in there that you can remove, its just design code for my site.  The code works based on just dates that have events, if a day doesnt have an event then there isnt a record for it to start with so the db could be just this

10/8/04 Appt 1
20/8/04 Appt 2
3/9/04 Appt 3

So there is no need to have a table with every date in it and theres just the one recordset holding the data.  Hope you can follow it easily enough...
if you have msn i will gladly give ya the files.   your both good at replying very quick, thanks. trying to go through code now.

msn: mcsqueeb @ hotmail dot com
Can't buddy...no MSN allowed at work....:(

I have uploaded it to a file server here is the link, must not be a busy day in work ah..... ;o)

http://s2.yousendit.com/d.aspx?id=AE2695BCE7CB6AFD0AD7492590CE9A35

thanks for your help.
You have an email...
you should be brought to a page where u can download the file???  works here and on the pcs around me
I downloaded it....made the fixes to my script, and emailed you the working page...
Here's a listing of the entire file, in case your email/my email isn't working:

Put this file in the same directory as the Access db/connection file....

<!--#include file="connection.asp"-->
<%
if request("givenDate") = "" then
  monthDate = Month(Now()) & "/1/2004"
else
  monthDate = request("givenDate")
end if
monthEndDay = DatePart("d",DateAdd("d",-1,DateAdd("m",1,CDate(monthDate))))
firstDay = DatePart("w",monthDate)  '1 = Sunday, 7 = Saturday
counter = 1
set rs = server.createObject("ADODB.RecordSet")

rs.Open "SELECT CalID as [primary key], CalDateFrom as [date of event] FROM tblCalendar ORDER BY CalDateFrom",objConnNNC,3,3
%>
<form name="changer" method="POST">
<input type="hidden" name="givenDate" value="<%=monthDate%>">
</form>
<table cellspacing="1" cellpadding="0" width="560">
  <tr>
    <td align="left" style="border : 1px solid black;"><a href="javascript: document.changer.givenDate.value = '<%=DateAdd("m",-1,CDate(monthDate))%>'; document.changer.submit();">&lt;&lt;</a></td>
    <td colspan="5" align="center" style="border : 1px solid black;">
      <%=MonthName(Month(monthDate))%>&nbsp;<%=Year(monthDate)%>
    </td>
    <td align="right" style="border : 1px solid black;"><a href="javascript: document.changer.givenDate.value = '<%=DateAdd("m",1,CDate(monthDate))%>'; document.changer.submit();">&gt;&gt;</a></td>
  </tr>
  <tr>
    <td style="border : 1px solid black;" width="80">
      Sun.
    </td>
    <td style="border : 1px solid black;" width="80">
      Mon.
    </td>
    <td style="border : 1px solid black;" width="80">
      Tue.
    </td>
    <td style="border : 1px solid black;" width="80">
      Wed.
    </td>
    <td style="border : 1px solid black;" width="80">
      Thurs.
    </td>
    <td style="border : 1px solid black;" width="80">
      Fri.
    </td>
    <td style="border : 1px solid black;" width="80">
      Sat.
    </td>
  </tr>
  <tr>
  <%
  for i=2 to firstDay
  %>
  <td>&nbsp;</td>
  <%
  next
  %>
  <%
  for i=firstDay to 7
  %>
  <td style="border : 1px solid black;" onMouseOver="this.style.backgroundColor = '#AAAAAA';" onMouseOut="this.style.backgroundColor = '#FFFFFF';" id="day_<%=counter%>"><%=counter%>
  <%
  if not rs.eof then
    Do      
      if rs.eof then
        Exit Do
      elseif DatePart("d",rs("date of event")) <> counter then
        Exit Do
      end if
      %>
     <a href="#<%=rs("primary key")%>"><img src="/common/images/calendar_item_online.gif" border="0"></a>                
      <script language="javascript">
      document.getElementById("day_<%=counter%>").className = "lightBlueCellSmall";
      document.getElementById("day_<%=counter%>").onmouseover = null;
      document.getElementById("day_<%=counter%>").onmouseout = null;
      </script>
      <%
      rs.moveNext
    Loop
  end if
  %>
 
  <BR><BR><BR></td>
  <%
    counter = counter + 1
  next
  weekDayCounter = 1
  %>
  </tr>
  <tr>
  <%
  while counter <= monthEndDay
    if weekDayCounter > 7 then
    %>
  </tr>
  <tr>
    <%
      weekDayCounter = 1
    end if
    %>
    <td style="border : 1px solid black;" onMouseOver="this.style.backgroundColor = '#AAAAAA';" onMouseOut="this.style.backgroundColor = '#FFFFFF';" id="day_<%=counter%>"><%=counter%>
    <%
  if not rs.eof then
    Do      
      if rs.eof then
        Exit Do
      elseif DatePart("d",rs("date of event")) <> counter then
        Exit Do
      end if

      %>
      <a href="#<%=rs("primary key")%>"><img src="/common/images/calendar_item_online.gif" border="0"></a>            
      <script language="javascript">
      document.getElementById("day_<%=counter%>").className = "lightBlueCellSmall";
      document.getElementById("day_<%=counter%>").onmouseover = null;
      document.getElementById("day_<%=counter%>").onmouseout = null;
      </script>
      <%
      rs.moveNext
    Loop
  end if
  %>
    <BR><BR><BR></td>
  <%
    weekDayCounter = weekDayCounter + 1
    counter = counter + 1
  wend
  for i=weekDayCounter to 7
  %>
    <td>&nbsp;</td>
  <%
  next
  %>
  </tr>
</table>
<%
rs.Close
set rs = Nothing
%>  
did you run my script and see the output?
Yeah, ran nice for me....I like the look of it
Thanks, I like the look to ;o)

Currently the calendar displays only the CalDateFrom field, so if you go to December the 25th will be blue, but the christmas hols run to the 2nd of Jan 2005.  I havent managed to figure a way of getting the sql / code to colour each square until both dates are <>

Looking at your code its nearly the same as mine, i just currently have the the database / sql inside the loop, i need to move it to before the loop.

SAMPLE SQL - with date range added
 
SELECT CalID, CalDateFrom, CalDateTo, CalType FROM tblCalendar WHERE CalDateFrom >= #" START OF MONTH "# AND CalDateTo <= #" END OF MONTH "# ORDER BY CalDateFrom DESC "

but when I introduce this code it dosent pick up the range and the script starts to fail ;o(       its now 7pm here and im wrecked lol
Don't bother....

Don't try to make the holidays like that, because, as you're no doubt finding out, its INSANELY difficult to do...

Instead, make each holiday one day long, and stretch the two (or more) of them together....it'll save you time and money...

Dan
but that isnt good database practice :o(     it also means the database will increase in size & searching will take longer with more cols/rows.

It also means the admin will have to input a new holiday a load of times which I dont think any client/user wants to do...   any ideas?    
No, its not good database practice, but good database practice doesn't always make for good programming practice, so there has to be some consideration there too...

How often do you forsee > 1 day holidays taking place? Twice/three times a year?  Now, think of all the time you're going to spend on buiding a much more complicated solution...in the end, you're better making it slightly less theoretically efficent to make it more efficient in practice...
granted there are only 8 bank holidays a year & 7/8 days of xmas but in the future they might want to add in events like meetings etc,   i will keep this post open for now, you shall get a good load of points dwaldner whatever the outcome :o)    if you think of anything please fire it at me, as you said i will prob just keep hols to 1 day max but I have spent this long trying I wouldnt mind finding out the proper outcome.
My only note would be the old maxim "Don't reinvent the wheel"....if you want an Outlook-esque calendar for scheduling meetings, use Outlook...

Glad to help...if you need anymore help, you've got my addy...feel free...

Dan