Avatar billede guzzie Nybegynder
28. september 2000 - 11:29 Der er 5 kommentarer og
1 løsning

Showing max 5 messages from an database

Hi There

I just started learning Asp last week and have been finding all sorts of scripts to use and play around with. This script is made up with a few of those (Cut and Paste). The script works fine but I wish to learn how or where I can limit the page to show only 5 messages.  Please Help.

Thanks
Guzzie
PS. I can read danish but my written danish is bad (This comes for me being English :)





<%

Const adOpenKeyset = 1
Const adLockOptimistic = 3

strMode = Request.QueryString(\"mode\")
tempConn = \"PROVIDER=MSDASQL;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=\" & Server.MapPath(\"nyheder.mdb\") & \";UID=admin;PWD=;\"

If Request.Form(\"Send\") <> \"\" Then
    strError = \"\"
    strPosted = Trim(Request.Form(\"Posted\"))
    strmail = Trim(Request.Form(\"mail\"))
    strNews = Trim(Request.Form(\"News\"))
    If Len(strPosted) = 0 Then
        strError = \"Write Your Name\"
    ElseIf validateEmail(strmail) = False Then
        strError = \"A Real E-mail Please\"
    ElseIf Len(strNews) = 0 Then
        strError = \"Message Please\"
    End If
    If Len(strError) = 0 Then
        Set rs = Server.CreateObject(\"ADODB.RecordSet\")
        strSQL = \"SELECT * FROM Nyheder WHERE (ID = 0)\"
        rs.Open strSQL, tempConn, adOpenKeyset, adLockOptimistic
        If (rs.BOF Or rs.EOF) Then
            rs.AddNew
            rs(\"Posted\") = Server.HTMLEncode(strPosted)
            rs(\"mail\") = Server.HTMLEncode(strmail)
            rs(\"News\") = Server.HTMLEncode(strNews)
            rs(\"Dato\") = Now()
            rs.Update
        End If
        rs.Close
        Set rs = Nothing
    Else
        strError =\"<ul><b>\" & strError & \"</b></ul>\"
        strMode = \"add\"
    End If
End If
%>
<html>

<head>
<title></title>
</head>
<body>
<LINK rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">
<table width=\"600\" align=\"center\" valign=\"top\" border=\"0\">
<tr>
    <td>
<%
Select Case strMode
Case \"add\"
%>
<form method=\"POST\" action=\"guestbook.asp\">
<table width=\"100%\" align=\"left\" valign=\"top\" border=\"1\">
<tr>
    <td bgcolor=\"#202020\" colspan=\"2\">POST A MESSAGE</td>
</tr>
<tr>
    <td><%=strError%></td>
</tr>
<tr>
    <td>Name</td><td><input type=\"text\" name=\"Posted\" size=\"40\" value=\"<%=strPosted%>\"><br><br></td>
</tr>
<tr>
    <td>E-mail</td><td><input type=\"text\" name=\"mail\" size=\"40\" value=\"<%=strmail%>\"><br><br></td>
</tr>
<tr>
    <td>Message</td><td><textarea rows=\"3\" name=\"News\" cols=\"40\"><%=strNews%></textarea></td>
</tr>
<tr>
    <td align=\"center\" colspan=\"2\"><br><br><input type=\"submit\" value=\"Send\" name=\"Send\"> &nbsp;<input type=\"button\" value=\"Annuller\" name=\"Annuller\" onClick=\"java script:location.href=\'guestbook.asp\'\"><br><br></td>
</tr>
<tr>
    <td><br><br><a href=\"java script:window.close()\">Close Window</a></td>
</tr>
</table>
</form>
<%
Case Else
    strAddLine = \"<a href=guestbook.asp?mode=add>Post Reply</a><br><br>\"
    Response.Write strAddLine
    strSQL = \"SELECT * FROM Nyheder ORDER BY Dato DESC;\"
    Set myConn = Server.CreateObject(\"ADODB.Connection\")
    myConn.Open tempConn
    Set rs = myConn.Execute(strSQL)
    If Not (rs.BOF Or rs.EOF) Then
        Response.Write \"<table border=1 cellpadding=5 cellspacing=1>\"
        Do While Not rs.EOF
            dtmDato = rs(\"Dato\")
            dtmDato = FormatDateTime(dtmDato,vbLongDate) & \" time \" & FormatDateTime(dtmDato,vbLongTime)
            Response.Write \"<tr><td bgcolor=#202020>\" & dtmDato & \"</td>\"
            If Len(rs(\"mail\")) > 0 Then
                Response.Write \"<td bgcolor=#202020 align=left> posted: <a href=mailto:\" & rs(\"mail\") & \">\" & rs(\"Posted\") & \"</a></td></tr>\"
           
            End If
            Response.Write \"<tr><td colspan=2>\" & rs(\"News\") & \"<br><br></td></tr>\"
            rs.MoveNext
        Loop
        Response.Write \"</table>\"
    Else
        Response.Write \"Nobody has a message\"
    End If
    myConn.Close
    Set myConn = Nothing
    Response.Write strAddLine
End Select
%>
</tr>
</table>
</div>
</body>
</html>
<script language=\"VBScript\" runat=\"Server\">
Function validateEmail(strmail)
    validateEmail = True
    If Len(strmail) < 6 Then
        validateEmail = False
    End If
    If InStr(1, strmail, \"@\", 1) < 2 Then
        validateEmail = False
    End If
    If InStr(1, strmail, \".\", 1) = 0 Then
        validateEmail = False
    End If
    pos = InStr(1, strmail, \"@\", 1)
    If pos < 2 Then
        validateEmail = False
            Else
        pos = InStr(pos+1, strmail, \"@\")
        If pos > 0 Then
            validateEmail = False
        End If
    End If
End Function
</script>
Avatar billede meyer Nybegynder
28. september 2000 - 11:33 #1
strSQL = \"SELECT TOP 5 * FROM Nyheder ORDER BY Dato DESC;\"
   
Avatar billede guzzie Nybegynder
28. september 2000 - 13:43 #2
I replaced the strSQL like you from this
strSQL = \"SELECT * FROM Nyheder WHERE (ID = 0)\"

to this

strSQL = \"SELECT TOP 5 * FROM Nyheder ORDER BY Dato DESC;\"

However it still does not work...
Any suggestions?
Avatar billede michael_ingeman Nybegynder
13. oktober 2000 - 11:40 #3
What happens when you run the script?
Avatar billede guzzie Nybegynder
13. oktober 2000 - 12:18 #4
Hej michael

When i changed the script from
select to
select top 5 the script still showed all the news items from my DB :(
hmmm
Avatar billede guzzie Nybegynder
13. oktober 2000 - 12:23 #5
Hej igen michael

BTW i am now using another script where that works with my DB
But still its was a little strange that it would not work
Avatar billede michael_ingeman Nybegynder
13. oktober 2000 - 12:54 #6
Yes. Quite strange. Btw. Did you change the strSQL both places? In the top AND in the middle of the script?
Avatar billede Ny bruger Nybegynder

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.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester