2

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...

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
user1049057
  • 459
  • 4
  • 15
  • 36
  • `` will only ever return the first row. As Jason stated, add the query attribute to the tag to loop over your recordset. – Dan Short Nov 16 '11 at 15:21

1 Answers1

2

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>
Jason
  • 1,957
  • 2
  • 20
  • 34