ColdFusion inital value of currentrow when no index specified in cfloop
Asked Answered
H

2

6

I am converting a ColdFusion application to C# (I'm a CF n00b).

I have a script that performs a cfquery and then cfloop's through the results, and it appears to be trying to compare the current row to its following row. And it appears to be trying to make sure that it doesnt try to read past the end of the array.

<cfquery name="qTripLegs" datasource="#sdb#">
   SELECT ...
</cfquery>

<cfloop query="qTripLegs">
    <cfif (customs_stop[currentrow] NEQ "" OR fuel_stop[currentrow] NEQ "") AND recordcount GT currentrow AND departure[currentrow] NEQ arrival[currentrow+1]>

It feels like currentrow is 1-based (currentrow will have a value of 1 when it first enters the cfloop). Am I correct? I have looked in the coldfusion documentation and I dont see anything about this.

Hyperplasia answered 8/1, 2013 at 23:10 Comment(0)
W
22

Yes, queries and arrays in CF are 1-based.

The CurrentRow and RecordCount variables are properties of the query (inside a query loop they are automatically scoped).

<cfloop query="QueryName">...</cfloop> will loop through the entire query*, from 1 to QueryName.RecordCount, and the QueryName.CurrentRow index is automatically populated/incremented appropriately. Its value prior to query loop isn't used.

*(unless cfbreak/etc used)

Also to point out there is generally no need to prevent reading past the end (as above, the query loop handles it), it's only because CurrentRow+1 is being used that it's needed to avoid an error.

Winker answered 9/1, 2013 at 0:30 Comment(1)
What about a reference to the row itself ?Maidamaidan
F
0

query.currentRow() Returns the current row number

queryCurrentRow(query) → returns numeric Member Function Syntax

<cfscript>
var myQuery = queryNew("id,title","integer,varchar",[[1,"Charlottes Web"],[3,"The Outsiders"],[4,"Mieko and the Fifth Treasure"]]);
cfloop(query = "myQuery"){
    if (title Eq "Mieko and the Fifth Treasure"){
        writeOutput(myQuery.currentRow());
    }
}
</cfscript>
Fodder answered 4/12, 2022 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.