queryEach

Iterates over query rows and passes each row per iteration to a callback function

queryEach(query, function(row [, currentRow] [, query] ){} [, parallel] [, maxThreads]) → returns void

Member Function Syntax

query.each(function(row [, currentRow] [, query] ){} [, parallel] [, maxThreads])

Argument Reference

query query
Required

query to loop over

callback any
Required

Closure or a function reference that will be called for each of the iteration.

Callback parameters:

  • row*struct : A struct with all of the columns for the current iteration
  • currentRow*numeric : The value for the current iteration
  • query*query : A reference of the original struct

parallel boolean

Lucee 4.5+ Executes closures parallel

maxThreads numeric
Default: 20

Lucee 4.5+ Maximum number of threads executed
If parallel argument is set to false it will be ignored

Compatibility

Lucee:

When called as a member function query.each() is chainable.

Links more information about queryEach

Examples
Sample code invoking the queryEach function

ACF 2016+ and Lucee 5+

<cfscript>
    news = queryNew("id,title",
    	"integer,varchar", [{
    		"id": 1,
    		"title": "Dewey defeats Truman"
    	}, {
    		"id": 2,
    		"title": "Man walks on Moon"
    	}]
    );
    function newsRow(row) {
        writeOutput("<tr>");
            writeOutput("<td>#row.id#</td>");
            writeOutput("<td>#row.title#</td>");
        writeOutput("</tr>");
    }
</cfscript>
<table>
    <cfscript>
        queryEach(news,newsRow);
    </cfscript>
</table>

Expected Result: 1 Dewey defeats Truman 2 Man walks on Moon

Lucee 5+

<cfscript>
    news = queryNew("id,title",
    	"integer,varchar", [{
    		"id": 1,
    		"title": "Dewey defeats Truman"
    	}, {
    		"id": 2,
    		"title": "Man walks on Moon"
    	}]
    );
    function newsRow(row) {
        writeOutput("<tr>");
            writeOutput("<td>#row.id#</td>");
            writeOutput("<td>#row.title#</td>");
        writeOutput("</tr>");
    }
</cfscript>
<table>
    <cfoutput>#queryEach(news,newsRow)#</cfoutput>
</table>

Expected Result: 1 Dewey defeats Truman 2 Man walks on Moon

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub