frequent
frequent

Reputation: 28513

How to loop a Coldfusion cfquery in Cfscript?

I'm trying to loop a query in cfscript. I think I have it, but it the query loops into infinity.

Can someone tell me what is wrong in the following:

<cfscript>
    // loop single msg
    variables.allRows = current_message.recordcount;
    for ( variables.intRow = 1 ; variables.allRows LTE variables.intRow ; variables.intRow = variables.intRow + 1 ){
        variables.msg_id_viewed = current_message[ "com_msg_id" ][ variables.intRow ];
        variables.msg_app_alias = current_message[ "com_app_alias" ][variables.intRow];
        variables.msg_img_ext = current_message[ "com_img" ][ variables.intRow ];
        }
</cfscript>

The query current_message returns a single record, so this should loop once only.

Thanks for help!

Upvotes: 2

Views: 8122

Answers (1)

Adam Cameron
Adam Cameron

Reputation: 29870

You have your condition around the wrong way.

It should be:

variables.intRow LTE variables.allRows 

I suspect current_message actually has multiple rows. Did you actually check, or did you simply run on the assumption that that's what you're expecting? Because it looks to me like variables.allRows LTE variables.intRow is evaluating to false, which suggests variables.allRows is greater than one when you first start.

In regards to this:

The query current_message returns a single record, so this should loop once only.

If there's only one row - and you know this to be the case - why are you looping?

That said, what version of ColdFusion are you on? In CF10 one can loop over a recordset with the for/in construct, thus:

for (row in recordset){
    // row is a struct keyed on each column name, the values being the value for that column/row
}

Upvotes: 11

Related Questions