Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Snitz Forums 2000 MOD-Group
 MOD Add-On Forum (W/Code)
 Random Records
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

GauravBhabu
Advanced Member

4288 Posts

Posted - 11 February 2002 :  00:06:46  Show Profile
One or More Random Record(s) From Database

Author: GauravBhabu (Rakesh Jain)
Upload: February 11, 2002 00:05:00
Update: February 12, 2002 18:35: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 %>
<%
dim sStrSql, sRstRandom
dim sIntMaxRecs
dim sIntTotalRecs,sRndRecList


set sRstRandom = server.CreateObject("adodb.recordset")

sStrSql = "SELECT T.TOPIC_ID, " & _
"T.T_SUBJECT FROM " & strTablePrefix & "TOPICS T"

sRstRandom.Open sStrSql,My_Conn,adOpenKeyset


'Change this value to the desired number
'of Random Records to be retrieved
sIntMaxRecs = 6

if not sRstRandom.EOF 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
sIntTotalRecs = sRstRandom.recordcount
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
do while not sRstRandom.EOF
Call DisplayRecord()
sRstRandom.MoveNext
Loop
end if
Response.write vbNewLine & _
"</table>" & vbNewLine
end if
'Close recordset
sRstRandom.Close
set sRstRandom= nothing

sub FindRandomRecords()
Dim RndRec,blnRecFound,arrRndList,iRec,jRec
Randomize()
for iRec = 0 to (sIntMaxRecs - 1)
'Generate a number to find a record in the recordset
RndRec = Int((sIntTotalRecs - 1 + 1) * Rnd + 1)
sRstRandom.Move (RndRec - 1)
if not sRstRandom.eof then
blnRecFound = true
else
blnRecFound = false
end if
if blnRecFound then
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
end if
if blnRecFound then
if iRec = 0 then
sRndRecList = RndRec
else
sRndRecList = sRndRecList & "#" & RndRec
end if
call DisplayRecord()
else
iRec = (iRec - 1)
end if
sRstRandom.MoveFirst
next
end sub

sub DisplayRecord()
Response.Write vbNewLine & _
" <tr bgColor=""" & strForumcellColor & """>" & vbNewLine & _
" <font face=""" & strDefaultFontFace & """ size=""" & strDefaultFontSize & """ Color=""" & strForumFontColor & """>" & vbNewLine & _
" <td>" & sRstRandom.Fields(0) & "</td>" & vbNewLine & _
" <td>" & vbNewLine & _
" <a href=""link.asp?TOPIC_ID=" & sRstRandom.Fields(0) & """>" & sRstRandom.Fields(1) & "</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:40:38

GauravBhabu
Advanced Member

4288 Posts

Posted - 11 February 2002 :  02:35:30  Show Profile
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
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.2 seconds. Powered By: Snitz Forums 2000 Version 3.4.07