Jag har en kalender som jag vill koppla till min databas. När man trycker på ett datum så ska aktuellt inlägg hämtas som postades då.
Koden jag har är:
Function GetDaysInMonth(iMonth, iYear)
Dim dTemp
dTemp = DateAdd("d", -1, DateSerial(iYear, iMonth + 1, 1))
GetDaysInMonth = Day(dTemp)
End Function
Function GetWeekdayMonthStartsOn(dAnyDayInTheMonth)
Dim dTemp
dTemp = DateAdd("d", -(Day(dAnyDayInTheMonth) - 1), dAnyDayInTheMonth)
GetWeekdayMonthStartsOn = WeekDay(dTemp)
End Function
Function SubtractOneMonth(dDate)
SubtractOneMonth = DateAdd("m", -1, dDate)
End Function
Function AddOneMonth(dDate)
AddOneMonth = DateAdd("m", 1, dDate)
End Function
' ***End Function Declaration***
Dim dDate ' Date we're displaying calendar for
Dim iDIM ' Days In Month
Dim iDOW ' Day Of Week that month starts on
Dim iCurrent ' Variable we use to hold current day of month as we write table
Dim iPosition ' Variable we use to hold current position in table
' Get selected date. There are two ways to do this.
' First check if we were passed a full date in RQS("date").
' If so use it, if not look for seperate variables, putting them togeter into a date.
' Lastly check if the date is valid...if not use today
If IsDate(Request.QueryString("date")) Then
dDate = CDate(Request.QueryString("date"))
Else
If IsDate(Request.QueryString("month") & "-" & Request.QueryString("day") & "-" & Request.QueryString("year")) Then
dDate = CDate(Request.QueryString("month") & "-" & Request.QueryString("day") & "-" & Request.QueryString("year"))
Else
dDate = Date()
' The annoyingly bad solution for those of you running IIS3
If Len(Request.QueryString("month")) <> 0 Or Len(Request.QueryString("day")) <> 0 Or Len(Request.QueryString("year")) <> 0 Or Len(Request.QueryString("date")) <> 0 Then
Response.Write "<div id='saknas'>Det valda datumet är inte ett gilltigt datum!</div>"
End If
End If
End If
'Now we've got the date. Now get Days in the choosen month and the day of the week it starts on.
iDIM = GetDaysInMonth(Month(dDate), Year(dDate))
iDOW = GetWeekdayMonthStartsOn(dDate)
%>
<table id="kalender_tabell">
<tr>
<td id="kalender_pil_vanster"><a href="./index.asp?do=blogg&date=<%= SubtractOneMonth(dDate) %>"><<</a></td>
<td id="kalender_aktuellt_datum" colspan="5"><%= MonthName(Month(dDate)) & " " & Year(dDate) %></td>
<td id="kalender_pil_hoger"><a href="./index.asp?do=blogg&date=<%= AddOneMonth(dDate) %>">>></a></td>
</tr>
<tr id="kalender_dagar">
<td class="cell_storlek">Sö</td>
<td class="cell_storlek">Mo</td>
<td class="cell_storlek">Ti</td>
<td class="cell_storlek">On</td>
<td class="cell_storlek">To</td>
<td class="cell_storlek">Fr</td>
<td class="cell_storlek">Lö</td>
</tr>
<%
' Write spacer cells at beginning of first row if month doesn't start on a Sunday.
If iDOW <> 1 Then
Response.Write vbTab & "<tr>" & vbCrLf
iPosition = 1
Do While iPosition < iDOW
Response.Write vbTab & vbTab & "<td> </td>" & vbCrLf
iPosition = iPosition + 1
Loop
End If
' Write days of month in proper day slots
iCurrent = 1
iPosition = iDOW
Do While iCurrent <= iDIM
' If we're at the begginning of a row then write TR
If iPosition = 1 Then
Response.Write vbTab & "<tr>" & vbCrLf
End If
' If the day we're writing is the selected day then highlight it somehow.
If iCurrent = Day(dDate) Then
Response.Write vbTab & vbTab & "<td id='kalender_markeratdatum'>" & iCurrent & "</td>" & vbCrLf
Else
Response.Write vbTab & vbTab & "<td class='kalender_ejmarkeratdatum'><a href=""./index.asp?do=blogg&date=" & Month(dDate) & "-" & iCurrent & "-" & Year(dDate) & """>" & iCurrent & "</a></td>" & vbCrLf
End If
' If we're at the endof a row then write /TR
If iPosition = 7 Then
Response.Write vbTab & "</tr>" & vbCrLf
iPosition = 0
End If
' Increment variables
iCurrent = iCurrent + 1
iPosition = iPosition + 1
Loop
' Write spacer cells at end of last row if month doesn't end on a Saturday.
If iPosition <> 1 Then
Do While iPosition <= 7
Response.Write vbTab & vbTab & "<td class='kalender_ejmarkeratdatum'></td>" & vbCrLf
iPosition = iPosition + 1
Loop
Response.Write vbTab & "</tr>" & vbCrLf
End If
När man trycker på ett datum så får det formatet 1-31-2006.
Hur löser man så att posten som är skriven då hämtas?
SQL satsen jag har är:
SELECT TOP 4 blogg.bloggID, blogg.bloggkatID, blogg.bloggRubrik, blogg.bloggMeddelande, blogg.bloggDatum, blogg_kategorier.bkatID, blogg_kategorier.bkatNamn FROM blogg_kategorier INNER JOIN blogg ON blogg_kategorier.bkatID = blogg.bloggkatID WHERE bloggDatum = ?????
Om du använder någon databas som inte heter Access (jag bara antog, eftersom du inte skriver vad du använder), så får du använda apostrofer istället runt datumet.
ok, har nu byggt om den till datumförmatet 2007-1-14.
Men jag får inte den att hämta en post när man trycker på ett datum då jag vet att det finns ett inlägg. anv. Access, datumformatet där är 2006-12-31 17:35:14.
Innehåller fältet i tabellen både datum och tid så får du använda datumfunktioner som year(), month() och day() i din sql-fråga för att bara hämta på rätt datum.
WHERE Year(bloggDatum) = Year("&Request.Querystring("date")&") AND Month(bloggDatum) = Month("&Request.Querystring("date")&") AND Day(bloggDatum) = Day("&Request.Querystring("date")&")
WHERE Year(bloggDatum) = Year("&Request.Querystring("date")&") AND Month(bloggDatum) = Month("&Request.Querystring("date")&") AND Day(bloggDatum) = Day("&Request.Querystring("date")&")
men den hittar fortfarande inget :OO
Du har använt funktionerna lite tokigt. Flytta ut Year etc. till VBScript..
Year(bloggDatum)=" & Year(datumet) & " AND ......
...eller datumet in till SQL:en
Year(bloggDatum)=Year(#" & datumet & "#) AND ......
tack, nu fungerar det. Nu undrar jag om det går att göra så bara de datumen som finns i databasen är markerade på något sätt (är klickbara), nu är alla klickbara. Hur löser jag detta??.
koden till kalendern är:
Function GetDaysInMonth(iMonth, iYear)
Dim dTemp
dTemp = DateAdd("d", -1, DateSerial(iYear, iMonth + 1, 1))
GetDaysInMonth = Day(dTemp)
End Function
Function GetWeekdayMonthStartsOn(dAnyDayInTheMonth)
Dim dTemp
dTemp = DateAdd("d", -(Day(dAnyDayInTheMonth) - 1), dAnyDayInTheMonth)
GetWeekdayMonthStartsOn = WeekDay(dTemp)
End Function
Function SubtractOneMonth(dDate)
SubtractOneMonth = DateAdd("m", -1, dDate)
End Function
Function AddOneMonth(dDate)
AddOneMonth = DateAdd("m", 1, dDate)
End Function
' ***End Function Declaration***
Dim dDate ' Date we're displaying calendar for
Dim iDIM ' Days In Month
Dim iDOW ' Day Of Week that month starts on
Dim iCurrent ' Variable we use to hold current day of month as we write table
Dim iPosition ' Variable we use to hold current position in table
' Get selected date. There are two ways to do this.
' First check if we were passed a full date in RQS("date").
' If so use it, if not look for seperate variables, putting them togeter into a date.
' Lastly check if the date is valid...if not use today
If IsDate(Request.QueryString("date")) Then
dDate = CDate(Request.QueryString("date"))
Else
If IsDate(Request.QueryString("year") & "-" & Request.QueryString("month") & "-" & Request.QueryString("day")) Then
dDate = CDate(Request.QueryString("year") & "-" & Request.QueryString("month") & "-" & Request.QueryString("day"))
Else
dDate = Date()
' The annoyingly bad solution for those of you running IIS3
If Len(Request.QueryString("year")) <> 0 Or Len(Request.QueryString("month")) <> 0 Or Len(Request.QueryString("day")) <> 0 Or Len(Request.QueryString("date")) <> 0 Then
Response.Write "<div id='saknas'>Det valda datumet är inte ett gilltigt datum!</div>"
End If
End If
End If
'Now we've got the date. Now get Days in the choosen month and the day of the week it starts on.
iDIM = GetDaysInMonth(Month(dDate), Year(dDate))
iDOW = GetWeekdayMonthStartsOn(dDate)
%>
<table id="kalender_tabell">
<tr>
<td id="kalender_pil_vanster"><a href="./index.asp?do=blogg&date=<%= SubtractOneMonth(dDate) %>"><<</a></td>
<td id="kalender_aktuellt_datum" colspan="5"><%= MonthName(Month(dDate)) & " " & Year(dDate) %></td>
<td id="kalender_pil_hoger"><a href="./index.asp?do=blogg&date=<%= AddOneMonth(dDate) %>">>></a></td>
</tr>
<tr id="kalender_dagar">
<td class="cell_storlek">Sö</td>
<td class="cell_storlek">Mo</td>
<td class="cell_storlek">Ti</td>
<td class="cell_storlek">On</td>
<td class="cell_storlek">To</td>
<td class="cell_storlek">Fr</td>
<td class="cell_storlek">Lö</td>
</tr>
<%
' Write spacer cells at beginning of first row if month doesn't start on a Sunday.
If iDOW <> 1 Then
Response.Write vbTab & "<tr>" & vbCrLf
iPosition = 1
Do While iPosition < iDOW
Response.Write vbTab & vbTab & "<td> </td>" & vbCrLf
iPosition = iPosition + 1
Loop
End If
' Write days of month in proper day slots
iCurrent = 1
iPosition = iDOW
Do While iCurrent <= iDIM
' If we're at the begginning of a row then write TR
If iPosition = 1 Then
Response.Write vbTab & "<tr>" & vbCrLf
End If
' If the day we're writing is the selected day then highlight it somehow.
If iCurrent = Day(dDate) Then
Response.Write vbTab & vbTab & "<td id='kalender_markeratdatum'>" & iCurrent & "</td>" & vbCrLf
Else
Response.Write vbTab & vbTab & "<td class='kalender_ejmarkeratdatum'><a href=""./index.asp?do=blogg&date=" & Year(dDate) & "-" & Month(dDate) & "-" & iCurrent & """>" & iCurrent & "</a></td>" & vbCrLf
End If
' If we're at the endof a row then write /TR
If iPosition = 7 Then
Response.Write vbTab & "</tr>" & vbCrLf
iPosition = 0
End If
' Increment variables
iCurrent = iCurrent + 1
iPosition = iPosition + 1
Loop
' Write spacer cells at end of last row if month doesn't end on a Saturday.
If iPosition <> 1 Then
Do While iPosition <= 7
Response.Write vbTab & vbTab & "<td class='kalender_ejmarkeratdatum'></td>" & vbCrLf
iPosition = iPosition + 1
Loop
Response.Write vbTab & "</tr>" & vbCrLf
End If
%>
</table>
269 ms totalt · 4 externa anrop · v20260731065814-full.1dc6f849