Reputation: 459
I am new to coldfusion and I am stuck with looping a query within a function. For example, I have a function within which has a query which returns the names which starts with 'a'. but i am able to get only one value (first value) from the database.Actually in the db we have more than 1 values for this query.How should i loop the query within the function? Any help is appreciated...
<cffunction name="getNames" returntype="any">
<cfargument name="letter" required="true">
<cfquery name="getNamesfrmDB" datasource="test">
select * from employee where firstname like '#arguments.letter#%'
</cfquery>
<cfreturn getNamesfrmDB/>
</cffunction>
<cfoutput>#getNames('a').firstname#</cfoutput>
Thanks in advance...
Upvotes: 2
Views: 3611
Reputation: 1977
ahh. I ready your question wrong... disregard previous answer..
You are passing the query straight out of the function, so it will be coming out as a query and you can treat it as such.
Use query="qname" in your cfouptut
<cffunction name="getNames" returntype="any">
<cfargument name="letter" required="true">
... your query ..
<cfreturn getNamesfrmDB/>
</cffunction>
<!---call the function--->
<cfset names = getNames('a')>
<!---now loop over the results using cfoutput--->
<cfoutput query="names">
<p>#firstname#</p>
</cfoutput>
<!---OR ALTERNATIVELY, as you can't use cfoutput inside cfoutput.. so if you are already inside a cfouput, you can also output query results using cfloop--->
<cfoutput>
..some other stuff...
<cfloop query="names">
<p>#firstname#</p>
</cfloop>
..some other stuff..
</cfoutput>
Upvotes: 2