2011-11-16 200 views
2

我是coldfusion的新手,我在循环查询函数中遇到困难。例如,我有一个函数,其中有一个查询返回以'a'开头的名称。但我能从数据库中只获得一个值(第一个值)。实际上,在db中,我们有超过1个值的查询。我应该如何在函数中循环查询? 任何帮助表示赞赏...预先显示查询结果

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

谢谢...

+0

''

使用query = “qname的” 将只返回第一行。正如Jason所说的,将查询属性添加到标记以循环记录集。 –

回答

2

啊。我准备好你的问题了......忽略以前的答案。

你正在将查询直接传递给函数,所以它会作为查询出现,你可以将它当作查询来处理。在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>