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!

Randrange without same id 1

Status
Not open for further replies.

coldvision

Programmer
Feb 12, 2001
3
NL
Hello everybody,
Could somebody please help me out?
Here's my prob!

When i select an id at random, with the 'RandRange' string,
How can I avoid having the same id again in the same query?

e.g.

<cfquery name=&quot;pull_id&quot; datasource=&quot;MySrc&quot; dbtype=&quot;ODBC&quot;>
SELECT *
FROM MyTable
WHERE id=#RandRange(1,96)#
</cfquery>

<cfoutput query=&quot;pull_id&quot;>
#id#
<form action=&quot;same_page.cfm&quot; method=&quot;POST&quot;>
<input type=&quot;hidden&quot; name=&quot;id&quot; value=#id#>
<input type=&quot;submit&quot; value=&quot; pull again! &quot;>
</form>
</cfoutput>

I created some workarounds but neither of them really satisfy.

tnx
 
-> either use another function (one of your own !!) than randrange to calculate the id
-> or use randomize THEN rand (i think it's the proper way)
-> or first calculate id, then find a way to compare it to its previous values, then if it has already be drawn, draw again. But this kind be time consumming i guess
 
Here's how I did it, getting 3 unique results from a catalog of rings:

<cfinclude template=&quot;qry_getAllRings.cfm&quot;>

make a list of values from that query
<cfset RingList = ValueList(getAllRings.RingID)>

then start the &quot;already used&quot; list
<cfset RingsUsed = 0>

now loop through the query list, setting &quot;to&quot; equal to how many results you want
<cfloop index=&quot;looper&quot; from=&quot;1&quot; to=&quot;3&quot;>

pick a random position in the query list
<cfset ListPosition = RandRange(1,ListLen(RingList))>

get the value at that position
<cfset attributes.ring = ListGetAt(RingList,ListPosition)>

then check to be sure that value hasn't been taken yet.
<cfloop condition=&quot;ListContains(RingsUsed,attributes.ring)&quot;>

if it has, then it'll grab another position/value. this loop will keep going as long as the result is found in the list of values already used.
<cfset ListPosition = RandRange(1,ListLen(RingList))>
<cfset attributes.ring = ListGetAt(RingList,ListPosition)>

ok, we've got our guaranteed-unique value
</cfloop>

then i query to get the other info associated with that result
<cfinclude template=&quot;qry_ringinfo.cfm&quot;>

and display it
<cfoutput>
#Ring#
</cfoutput>

then add the just-displayed result's value to the &quot;already-used&quot; list
<cfset RingsUsed = ListAppend(RingsUsed,attributes.ring)>

and start over again
</cfloop>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top