View comments | RSS feed

cfbreak

Description

Used within a cfloop tag. Breaks out of a loop.

Category

Flow-control tags

Syntax

<cfbreak>

See also

cfabort, cfexecute, cfif, cflocation, cfloop, cfthrow, cftry; cfloop and cfbreak in Elements of CFML in ColdFusion MX Developer's Guide

Example

<!--- This shows the use of cfbreak to exit a loop when a condition is met.--->
<!--- Select courses; use cfloop to find a condition; then break the loop. --->
<!--- Check that number is numeric. --->
<cfif IsDefined("form.course_number")>
   <cfif Not IsNumeric(form.course_number)>
      <cfabort> 
   </cfif> 
</cfif> 
<cfquery name="GetCourses" datasource="cfdocexamples">
   SELECT * 
   FROM Courses 
   ORDER by course_number 
</cfquery>

<p> This example uses CFLOOP to cycle through a query to find a value.
(In our example, a list of values corresponding to courses in the Snippets
datasource). When the conditions of the query are met, CFBREAK stops the loop.
<p> Please enter a Course Number, and hit the "submit" button: 
<form action="cfbreak.cfm" method="POST"> 
   <select name="courseNum"> 
      <cfoutput query="GetCourses"> 
         <option value="#course_number#">#course_number#
      </cfoutput> 
   </select> 
   <input type="Submit" name="" value="Search on my Number"> 
</form> 
<!--- If the courseNum variable is not defined, 
      don't loop through the query.--->
<cfif IsDefined ("form.courseNum") IS "True">
<!--- Loop through query until value found, then use CFBREAK to exit query.--->
   <cfloop query="GetCourses"> 
      <cfif GetCourses.course_number IS form.courseNum> 
         <cfoutput> 
            <h4>Your Desired Course was found:</h4> 
            <pre>#course_number# #descript#</pre>
         </cfoutput> 
         <cfbreak> 
      <cfelse> 
         <br> Searching... 
      </cfif> 
   </cfloop> 
</cfif> 

ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


jrunrandy said on Feb 9, 2005 at 7:02 AM :
The first paragraph says that you can use cfbreak within a cfswitch block. This is incorrect (doc bug 59732).

For some reason, CFMX 6.1 allowed cfbreak within cfswitch. However, CF 5, CFMX, and CFMX 7 do not.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000221.htm