• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Checking in a loop

LEGEND ,
Nov 30, 2006 Nov 30, 2006

Copy link to clipboard

Copied

What is the proper method the do 'this' while executing a loop.

Lets say you want to generate a random unique number.
First you create a random number and when it is created you want to check if
it is unique by checking it against the database.
If it isn't, loop again and try again.
If it is, then move to the next step of the code block.

What I can't figure out is how break out of the loop if the check is
correct.

Here is some of my code. How do I break out of the loop if the recordcount
of the 'checking' query EQ 0?

What I use to generate a random number:
<cfset ststring=structNew()>
<cfloop index="i" from="1" to="4" step="1">
<cfset a = randrange(0,9)>
<cfset ststring["#i#"]=#chr(a)#>
</cfloop>
<cfset exid ="#ststring[1]##ststring[2]##ststring[3]##ststring[4]#">

What I am going to use to check the number:
<cfquery name="checknumber" datasource="**">
SELECT * WHERE examid = #exid#
</cfquery>
<cfif checknumber.recordcount EQ 0> or <cfif checknumber.recordcount NEQ 0>


TOPICS
Advanced techniques

Views

188

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 30, 2006 Nov 30, 2006

Copy link to clipboard

Copied

What you are trying to do is called a conditional loop. The cfml reference manual gives you the syntax. If you don't have one, the internet does.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 01, 2006 Dec 01, 2006

Copy link to clipboard

Copied

LATEST
Thanks Dan for pointing me in the right direction. Figured it out.


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation