Any idea what is wrong with the syntax of this CFLOOP ?

cgttsp01 used Ask the Experts™
I am getting an error using this CFLOOP statement:
<CFLOOP query="Query_AB" from="1" index="i" to="#ListLen(INumber)#" > 

Here is the error message.
Any idea why?

Error Diagnostic Information
Attribute set validation error in tag CFLOOP

The tag has an invalid attribute combination: the most likely attribute combination is

Required attributes: 'FROM,INDEX,TO'. Optional attributes: 'STEP'.
Other possible combinations are
Required attributes: 'COLLECTION,ITEM'. Optional attributes: None.
Required attributes: 'INDEX,LIST'. Optional attributes: 'DELIMITERS'.
Required attributes: 'QUERY'. Optional attributes: 'ENDROW,STARTROW'.
Required attributes: 'CONDITION'. Optional attributes: None.
The error occurred while processing an element with a general identifier of (CFLOOP), occupying document position (8:1) to (8:60).
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

When you loop through a query, you don't specify your index.  You're actually combining an index loop and a query loop.

You could write your loop like so:

<CFLOOP query="Query_AB">

or if you wanted to do an index loop, you could loop through the query like so:

<cfloop index="i" from="1" to="recordcount.Query_AB">

and you could get elements in the query using the following:

Query_AB.column1[i], Query_AB.column2[i]

Where Column1 and Column2 are the names of the columns.


Your suggestion for: <cfloop index="i" from="1" to="#ListLen(Query_AB.INumber)#"> works fine and it corrected the error.

However, the loop is only getting the first record from the query 'Query_AB'.

Any idea on why it is not looping all records?

For the "to" paramater, you should use "#recordcount.Query_AB#" and not "#ListLen(Query_AB.INumber)#"

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

<cfloop index="i" from="1" to="Query_AB.recordcount">

i missed out #.

<cfloop index="i" from="1" to="#Query_AB.recordcount#">

Hi there,

If u want to loop on Query then

to use the query !!!
<cfloop query="queryname">

to use the list !!!
<cfloop index="i" from="1" to="#listlen(inumber)#">

now it depends waht u wanna use

let me know

actually it's #queryname.columnlist#

What is the action the looping is supposed to be doing, and how many records are returned by the query?

Also I find it usefull to declare a value for listlen(inumber) and then make sure it's greater than 0, before taking the cfloop action.

It all depends on the recordset and what actions you are doing with each loop.

The more you tell, the more we can help.

btw has all the cold fusion documentation from CF5 on.
If you're trying to loop through only a certian number of records in the query, to do something like "Next 15", or basically output on the top 25 records or soemthing, then you want to use startRow and endRow.

So from this:

<CFLOOP query="Query_AB" from="1" index="i" to="#ListLen(INumber)#" >

You get this:

<CFLOOP query="Query_AB" startRow="1" endRow="#ListLen(INumber)#">

This will loop through from the first to however many ListLen(INumber) evaluates to.
actually it's safer to not loop straight the listlen value of inumber

I would do it like this.

<cfparam name=endrow default=0>

Then down where we do the looping

<cfif listlen(inumber) gt 0>
   <cfset endrow = listlen(inumber)>

<CFLOOP query="Query_AB" startRow="1" endRow="#endrow#">

And this way you are making sure that there is valid data, and it won't error out if there is nothing in that field.


thanks Anand.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial