One or More Random Record(s) From Database USING GetRows Method
Author: GauravBhabu (Rakesh Jain)
Upload: February 11, 2002 00:05:00
Update: February 12, 2002 18:37:00
Demo: Random Records
What this does:
Displays one or More Random Record(s) from the database table.
How it works is:
- Gets a Record Count
- Calls a Procedure to Find Six Unique Random Records
- Number of Random Records to be picked can be changed
- Displays Records
Implementation:
- Create a new file and copy the following code to that file.
- Name it rndTopics.asp and save it to your forum directory to see it working
- Can be used to display the links on Home page - Get Creative
SourceCode:
<!--#INCLUDE File="config.asp" -->
<!--#INCLUDE File="inc_top_short.asp" -->
<%@ Language=VBScript %>
<%
'Declare Script Level variavles
dim sStrSql, sRstRandom
dim arrRandomRecs
dim sIntTotalRecs,sIntMaxRecs,sRndRecList
'Declare constants
Const adOpenForwardOnly = 0
Const adLockReadOnly = 1
Const adCmdText = &H0001
Const adGetRowsRest = -1
set sRstRandom = server.CreateObject("adodb.recordset")
sStrSql = "SELECT T.TOPIC_ID, " & _
"T.T_SUBJECT FROM " & strTablePrefix & "TOPICS T"
sRstRandom.Open sStrSql,My_Conn, adOpenForwardOnly, adLockReadOnly, adCmdText
if not sRstRandom.EOF then
sIntMaxRecs = 6
arrRandomRecs = sRstRandom.GetRows(adGetRowsRest)
sIntTotalRecs = ubound(arrRandomRecs,2)
else
sIntTotalRecs = ""
end if
'Close recordset
sRstRandom.Close
set sRstRandom= nothing
if sIntTotalRecs <> "" then
Response.Write vbNewLine & _
"<table bgcolor=""" & strPopUptableColor & """>" & vbNewLine & _
" <tr bgColor=""" & strCategoryCellColor & """>" & vbNewLine & _
" <font face=""" & strDefaultFontFace & """ size=""" & strDefaultFontSize & """ Color=""" & strCategoryFontColor & """>" & vbNewLine & _
" <td>Topic ID</td>" & vbNewLine & _
" <td>Topic Title</td>" & vbNewLine & _
" </font>" & vbnewLine & _
" </tr>" & vbNewLine
if sIntTotalRecs > sIntMaxRecs then
'----Write Random Records
Call FindRandomRecords()
else
'----Write all the records if number of records in the
'----database table is less than randomn records requested
for sIntLoop = 0 to sIntTotalRecs
Call DisplayRecord(sIntLoop)
next
end if
Response.Write vbNewLine & _
"</table>" & vbnewLine
end if
'This sub Finds Random Records
sub FindRandomRecords()
Dim RndRec,blnRecFound,arrRndList,iRec,jRec
Randomize()
for iRec = 0 to (sIntMaxRecs - 1)
'Generate a number to get a record from the array
RndRec = Int((sIntTotalRecs - 0 + 1) * Rnd + 0)
blnRecFound = true
if iRec > 1 then
arrRndList = split(sRndRecList,"#")
for jRec = 0 to (iRec - 1)
if cint(RndRec) = cint(arrRndList(jRec)) then blnRecFound = false : exit for
next
else
if iRec = 1 then
if cint(RndRec) = cint(sRndRecList) then blnRecFound = false
end if
end if
if blnRecFound then
if iRec = 0 then
sRndRecList = RndRec
else
sRndRecList = sRndRecList & "#" & RndRec
end if
Call DisplayRecord(RndRec)
else
iRec = (iRec - 1)
end if
next
end sub
'This sub displays the records
sub DisplayRecord(IntTemp)
Response.Write vbNewLine & _
" <tr bgColor=""" & strForumcellColor & """>" & vbNewLine & _
" <font face=""" & strDefaultFontFace & """ size=""" & strDefaultFontSize & """ Color=""" & strForumFontColor & """>" & vbNewLine & _
" <td>" & arrRandomRecs(0,IntTemp) & "</td>" & vbNewLine & _
" <td>" & vbNewLine & _
" <a href=""link.asp?TOPIC_ID=" & arrRandomRecs(0,IntTemp) & """>" & arrRandomRecs(1,IntTemp) & "</a>" & vbNewLine & _
" </td>" & vbNewLine & _
" </font>" & vbNewLine & _
" </tr>" & vbNewLine
end sub
%>
<!--#INCLUDE File="inc_footer_short.asp" -->
www.forumSquare.com - GauravBhabu - It is difficult to IMPROVE on Perfection, There is no harm in Keep Trying.
Edited by - GauravBhabu on 12 February 2002 18:41:58