2

我是 ColdFusion 的初学者,想从循环中将值附加到数组中。我已经编写了这段代码,但它对我不起作用。

<cfset myArray = arrayNew(1)>
<cfloop query="displayQ" >
    <cfquery name="fileListQ" datasource="#REQUEST.datasource#">
        select
            project_id,
            doc_id,
            file_name,
            file_size,
            status,
            status_date,
            timestamp,
            upload_date
        from project_documents
        where
             project_id = "#displayQ.project_id#"
             <cfif bitAnd(SESSION.rights,structFind(rightsList,"RIGHTS_ADMIN")) EQ 0 
                  AND bitAnd(SESSION.rights,structFind(rightsList,"RIGHTS_ENOVIS_PS")) EQ 0 >
                and status = 3
             </cfif>
    </cfquery>
    <cfloop query="fileListQ">
        <tr>
            <CFSET myArray=ArrayAppend(myArray,#fileListQ.doc_id#,"true"); />
            <td><span class="FAKELINK" onClick="doReport('#fileListQ.file_name#','#fileListQ.doc_id#')">
                     #fileListQ.file_name#
                </span>
            </td>
        </tr>
    </cfloop>
</cfloop>
4

1 回答 1

3

您没有描述代码如何不适合您,但我的猜测是在这一行。

<CFSET myArray=ArrayAppend(myArray, #fileListQ.doc_id#, "true"); />

ArrayAppend()您正在将函数调用的返回值设置为数组变量myArray,但该函数在成功或失败时返回一个布尔值。因此,您的数组将被调用的布尔返回值覆盖。看来您只需要将其更改为:

<CFSET booleanDidItWork=ArrayAppend(myArray, fileListQ.doc_id, "true") />

另请注意,#当变量用作这样的函数调用的一部分时,不需要井号。

使用这样的标签语法时不需要分号。只有在编写 cfscript 语法时才需要这些。

一些 ColdFusion 功能按照您尝试过的方式工作,但其他功能则不然。这就是为什么在尝试使用函数时需要阅读有关函数的文档的原因。

描述

将数组元素附加到数组。当 merge 参数设置为 true 并且 value 参数是一个数组时,连接数组。

退货

没错,成功完成。

类别

数组函数

函数语法

ArrayAppend(数组,值 [,合并])

来自ArrayAppend 文档

于 2017-03-07T14:44:25.870 回答