Search code examples
coldfusioncoldfusion-9

Coldfusion: How to loop over query from function


I have a query that gets called multiple times depending on the the variable vchrStatus

<cffunction name="getGalleriesByStatus" output="no" returntype="query">
    <cfargument name="vchrStatus" type="string" required="yes">
    <cfquery 
        name="getGalleries"
        datasource="#Application.dsn#">
        /// Long complicated query in here
    </cfquery>
    <cfset var result="#getGalleries#">
    <!--- Return it --->
    <cfreturn result>
</cffunction>

I tested the function with cfdump and it is outputing the desired results.

Now I want to loop over the results in a cfoutput

<cfoutput query="getGalleriesByStatus('Pending')">

But I'm getting the error: The value of the attribute query, which is currently getGalleriesByStatus('Pending'), is invalid.


Solution

  • <cfoutput> takes the name of the query variable (ie: a string). It does not take an expression which evaluates to a query. So you need this:

    <cfset someVar = getGalleriesByStatus('Pending')>
    <cfoutput query="someVar">
    

    This is counter-intuitive, but is the case. It's also not in the docs, which sux.