10. september 2002 - 10:22Der er
24 kommentarer og 1 løsning
At sortere via dato
Jeg har en database, som indeholder nogle nyhedsbreve, som gerne løbende skulle udvides med nye nyhedsbreve.
Jeg har sat mit recordset således, at jeg får vist de seneste 10 nyhedsbreve - jeg sorterer via dato.
Men det virker ikke. Jeg får vist poster i vilkårlig rækkefølge.
Nedenfor er der lidt kode...
<%@LANGUAGE="VBSCRIPT"%> <!--#include file="../../../Connections/Connelaw.asp" --> <% set Recordset1 = Server.CreateObject("ADODB.Recordset") Recordset1.ActiveConnection = MM_Connelaw_STRING Recordset1.Source = "SELECT * FROM elawalert ORDER BY elawalertdato ASC" Recordset1.CursorType = 0 Recordset1.CursorLocation = 2 Recordset1.LockType = 3 Recordset1.Open() Recordset1_numRows = 0 %> <% ' *** Go To Record and Move To Record: create strings for maintaining URL and Form parameters
' create the list of parameters which should not be maintained MM_removeList = "&index=" If (MM_paramName <> "") Then MM_removeList = MM_removeList & "&" & MM_paramName & "=" MM_keepURL="":MM_keepForm="":MM_keepBoth="":MM_keepNone=""
' add the URL parameters to the MM_keepURL string For Each Item In Request.QueryString NextItem = "&" & Item & "=" If (InStr(1,MM_removeList,NextItem,1) = 0) Then MM_keepURL = MM_keepURL & NextItem & Server.URLencode(Request.QueryString(Item)) End If Next
' add the Form variables to the MM_keepForm string For Each Item In Request.Form NextItem = "&" & Item & "=" If (InStr(1,MM_removeList,NextItem,1) = 0) Then MM_keepForm = MM_keepForm & NextItem & Server.URLencode(Request.Form(Item)) End If Next
' create the Form + URL string and remove the intial '&' from each of the strings MM_keepBoth = MM_keepURL & MM_keepForm if (MM_keepBoth <> "") Then MM_keepBoth = Right(MM_keepBoth, Len(MM_keepBoth) - 1) if (MM_keepURL <> "") Then MM_keepURL = Right(MM_keepURL, Len(MM_keepURL) - 1) if (MM_keepForm <> "") Then MM_keepForm = Right(MM_keepForm, Len(MM_keepForm) - 1)
' a utility function used for adding additional parameters to these strings Function MM_joinChar(firstItem) If (firstItem <> "") Then MM_joinChar = "&" Else MM_joinChar = "" End If End Function
Dim Repeat1__numRows Repeat1__numRows = 10 Dim Repeat1__index Repeat1__index = 0 Recordset1_numRows = Recordset1_numRows + Repeat1__numRows
ASC betyder da Ascending (stigende) DESC betyder jo Descending (faldende) Hvis du vil have de nyeste artikler først, skal det være Recordset1.Source = "SELECT * FROM elawalert ORDER BY elawalertdato DESC"
kan lave datoen om til et tal når det bliver indsat i db'en, og så noget i retning af "select top 10 .... order by dato desc" og så selvfølgelig lave tallet om til dato når du skriver ud... så er man udeover de problemer med dato formater :)
<%@LANGUAGE="VBSCRIPT"%> <!--#include file="../../../Connections/Connelaw.asp" --> <% set Recordset1 = Server.CreateObject("ADODB.Recordset") Recordset1.ActiveConnection = MM_Connelaw_STRING Recordset1.Source = "SELECT * FROM elawalert ORDER BY elawalertdato DESC" Recordset1.CursorType = 0 Recordset1.CursorLocation = 2 Recordset1.LockType = 3 Recordset1.Open() Recordset1_numRows = 0 %> <% ' *** Go To Record and Move To Record: create strings for maintaining URL and Form parameters
' create the list of parameters which should not be maintained MM_removeList = "&index=" If (MM_paramName <> "") Then MM_removeList = MM_removeList & "&" & MM_paramName & "=" MM_keepURL="":MM_keepForm="":MM_keepBoth="":MM_keepNone=""
' add the URL parameters to the MM_keepURL string For Each Item In Request.QueryString NextItem = "&" & Item & "=" If (InStr(1,MM_removeList,NextItem,1) = 0) Then MM_keepURL = MM_keepURL & NextItem & Server.URLencode(Request.QueryString(Item)) End If Next
' add the Form variables to the MM_keepForm string For Each Item In Request.Form NextItem = "&" & Item & "=" If (InStr(1,MM_removeList,NextItem,1) = 0) Then MM_keepForm = MM_keepForm & NextItem & Server.URLencode(Request.Form(Item)) End If Next
' create the Form + URL string and remove the intial '&' from each of the strings MM_keepBoth = MM_keepURL & MM_keepForm if (MM_keepBoth <> "") Then MM_keepBoth = Right(MM_keepBoth, Len(MM_keepBoth) - 1) if (MM_keepURL <> "") Then MM_keepURL = Right(MM_keepURL, Len(MM_keepURL) - 1) if (MM_keepForm <> "") Then MM_keepForm = Right(MM_keepForm, Len(MM_keepForm) - 1)
' a utility function used for adding additional parameters to these strings Function MM_joinChar(firstItem) If (firstItem <> "") Then MM_joinChar = "&" Else MM_joinChar = "" End If End Function
Dim Repeat1__numRows Repeat1__numRows = 10 Dim Repeat1__index Repeat1__index = 0 Recordset1_numRows = Recordset1_numRows + Repeat1__numRows
Hmmm....jeg har aldrig prøvet at bruge Recordset1.Fields.Item("elawalertdato").Value, kun Recordset1("elawalertdato"), så lad spørgsmålet stå for andre at svare på.....
hvis jeg var dig vill jeg ændre feltet i db'en til tal og lave datoen om til et tal når du indsætter..:
Dag = day(now) maaned = month(now) aar = year(now) IF Len(Dag) = 1 THEN Dag = 0 & Dag END IF IF Len(maaned) = 1 THEN maaned = 0 & maaned END IF dato = aar & maaned & dag så får du noget ligende det her .: 20020910
det kommer så ind i db'en hvis du så vil skrive det ud til brugeren som en dato gør du bare sådan her
response.write mid(rs("dato"),7,2) & "/" & mid(rs("dato"),5,2) & "/" & mid(rs("dato"),1,4) hvilket giver et resultat som det her : 10/09/2002
Det her laver du lige før du skal indsætte data i databasen
Dag = day(now) maaned = month(now) aar = year(now) IF Len(Dag) = 1 THEN Dag = 0 & Dag END IF IF Len(maaned) = 1 THEN maaned = 0 & maaned END IF dato = aar & maaned & dag
det her laver du hvor du nu engang skriver ud .: response.write mid(rs("dato"),7,2) & "/" & mid(rs("dato"),5,2) & "/" & mid(rs("dato"),1,4)
du sætter selvfølgelig dato variablen det sted i SQL'en der passer elelrs hjælper det jo ikek ret meget :)
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.