Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do you randomly select a something from a table

Status
Not open for further replies.

dunskii

Programmer
Joined
Sep 14, 2001
Messages
107
Location
AU
Hi again,

how do you randomly select something from a table. eg. table id

thanks again,

Dunskii
 
the way that i usually get a random record from a database is to run a query, to get all of the records from the database and then use randrange. this generates a number between a two numbers

I would use the following code:

<CFSET List = &quot;&quot;>
<CFLOOP query=&quot;queryname&quot;>
<CFSET List = #ListAppend(List,DatabaseID)#>
</CFLOOP>

<CFIF queryname.Recordcount GT 0>
<CFSET randRec = RandRange(1,ListLen(List))>
</CFIF>

then to output the information:
<cfoutput>
#queryname.fieldname[randRec]#
</cfoutput>

the use of the list is required so that we don't get a random number for a database record that doesn't exist.

hope this helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top