2011-11-16 26 views
2

私はcoldfusionを初めて使用しています。関数内でクエリをループすることに悩まされています。たとえば、 'a'で始まる名前を返すクエリがある関数があります。しかし私はデータベースから1つの値(最初の値)を取得することができます。実際には、私たちはこのクエリのために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

'' 中= "のQName"

使用クエリは今まで最初の行を返します。 Jasonが述べたように、タグにquery属性を追加して、レコードセットをループします。 –

答えて

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> 
関連する問題