2

我已经习惯了 ColdFusion。cfscript 似乎使开发人员的生活更轻松。

在我的 cfscript 函数中,我需要:

  1. 运行查询“从 MyTable 中选择 A、B”
  2. 对于此查询的每一行,动态创建一个具有三个属性 A、B 和 C 的新组件。这里,C 在函数内计算。
  3. 返回新组件的数组。

这是伪代码:

<cfquery name="myq" datasource="mydsn">
    SELECT A, B
   FROM MyTable
</cfquery>

<cfscript>
function MyFunc() {
   // Do the magic and return the array
} 
</cfscript>

我猜我会像使用查询一样使用这个函数:

<cfset myarray=MyFunc() />
<cfloop index="i" from="1" to="#arrayLen(myarray)#">
   #myarray.A# <br />
   #myarray.B# <br />
   #myarray.C# <br />
</cfloop>

如果您能建议我如何做到这一点,我将不胜感激。我一直在搜索 Adob​​e 文档,但没有找到一个可以与之相关的好例子。预先感谢您的帮助。

4

2 回答 2

2

我知道我不应该只是粘贴链接,但这确实是您所需要的。

Then just use new xxx() for new object, ArrayAppend() for constructing an array and return it.

Good luck, let us know if you run into any other problems.

于 2013-10-19T03:19:41.203 回答
1

You probably want something like

 <cfoutput>
 <cfloop index="i" from="1" to="#arrayLen(myarray)#">
  #myarray.A[i]# <br />
  #myarray.B[i]# <br />
  #myarray.C[i]# <br />
 </cfloop>
 </cfoutput>

Overall you should consider returning a query rather than an array

<cfscript>
  query function MyFunc() {
  // Do the magic and return the array
  } 
</cfscript>

Then process it by

 <cfset myQuery = MyFunc()>

 <cfoutput query="myQuery">
      #A# <br />
      #B# <br />
      #C# <br />
 </cfoutput>

One rule of thumb in ColdFusion is: Queries are more powerful than arrays of structs. You can process them that way, but you will miss out on some very power features such as having <cfoutput> iterating over a query.

于 2013-10-19T16:45:38.083 回答