Har en databas med ca. 400 poster och vill slumpa ut 20 st ur denna hur gör man detta ? Varje post får bara visas 1 gång !!! Hmm.. du får helt enkel slumpa tjugo tal och spara dem i en array. För varje tal kollar du om det redan finns finns det så slumpar du om det utan att spara det i arrayen.. Löste det med detta........TOP 20 + RANDOM
Sv: TOP 20 + RANDOM
Sv: TOP 20 + RANDOM
<!--#INCLUDE FILE="adovbs.inc" -->
<%
omgang=request("omgang")
maxnumber=request("antal")
Dim objConn
Dim objRst
Dim strSQL
Dim strConnection
Dim str
Dim str1
Dim cnt
Dim cnt1
Dim rndMax
Dim RndNumber
strConnection="driver={Microsoft Access Driver (*.mdb)};dbq=" & server.mappath("../db/db1.mdb")
strSQL = "SELECT * FROM user where user_td LIKE '"& omgang&"' "
set objConn = Server.CreateObject("ADODB.Connection")
Set objRst = Server.CreateObject("ADODB.Recordset")
objConn.Open strConnection
set objRst.ActiveConnection = objConn
objRst.LockType = adLockOptimistic
objRst.CursorType = adOpenKeySet
objRst.Open strSQL
objRst.MoveLast
cnt = objRst.RecordCount
cnt1 = cnt
rndMax = cnt
If cint(maxNumber) < cnt Then
cnt1 = cint(maxNumber)
End If
str = ","
str1 = ","
Do Until cnt1 = 0
Randomize
RndNumber = Int(Rnd * rndMax)
If (InStr(1, str1, "," & RndNumber & "," ) = 0) Then
str1 = str1 & RndNumber & ","
cnt1 = cnt1 - 1
objRst.MoveFirst
objRst.Move RndNumber
str = str & objRst("user_session") & ","
End If
Loop
objRst.Close
Set objRst = Nothing
sql = "SELECT * FROM user WHERE (((InStr(1,'" & str & "',(',' & [user_session] & ',')))<>0)) AND user_td LIKE '"& omgang&"' "
Set objRst = Server.CreateObject("ADODB.Recordset")
set objRst.ActiveConnection = objConn
objRst.LockType = adLockOptimistic
objRst.CursorType = adOpenKeySet
objRst.Open sql
%>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<% do while not objRst.eof
count=count+1 %>
<%=count%>.<%=objRst("user_namn")%><br><%=objRst("user_slogan")%><br>
<% objRst.movenext
loop %>
<%
objRst.Close
Set objRst = Nothing
objConn.Close
Set objConn = Nothing
%>
</body>
</html>