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!

At random + cfquery + cfloop/cfoutput?

Status
Not open for further replies.

Bramvg

IS-IT--Management
Jan 16, 2001
135
BE
Hi there,

I hope someone can help me out.

A cfquery result returns 35 records (this is not always 35).
One fieldname 'QuestionID' contains numbers of questions. Now, I would like to make a list of 6 question, randomly picked from the 35 (result of the query).

All 6 (at random picked) questions must be unique! The same questionID may not be repeated in the list.

Does anyone of you have a suggestion?

THANK YOU VERY MUCH.

bram
 
this should do it:

<CFSET listOfIDs = &quot;&quot;>
<CFSET FinalQuestID = &quot;&quot;>
<CFSET x = 1>

<CFLOOP QUERY=&quot;yourquery&quot;>
<!--- set up the list of QuestionID's --->
<CFSET listOfIDs = ListAppend(ListOfIDs, yourQuery.QuestionID)>
</CFLOOP>
<CFLOOP CONDITION=&quot;x LESS THAN OR EQUAL TO 6&quot;>
<!--- create a random number from 1 to the size of your list ---->
<CFSET RandNum = RandRange(1,ListLen(ListOFIDs))>
<!--- get the element at that position from the list --->
<CFSET ListQID = ListGetAt(listOfIDs,RandNum)>

<!--- check to see if it exists --->
<CFIF ListContains(FinalQuestID, ListQID) EQ 0>
<!--- it doesn't exists so add it --->
<CFSET FinalQuestID = ListAppend(FinalQuestID,ListQID)>
<!--- increment x --->
<CFSET x = x + 1>
</CFIF>
</CFLOOP>

<cfoutput>
#FinalQuestID#
</CFOUTPUT>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top