如何在 coldfusion 中附加来自 <cfloop> 的数组值?

how to append array value from < cfloop> in coldfusion?

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

你没有描述代码为什么不适合你,但我的猜测是在这一行。

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

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

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

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

并且像这样使用标记语法时不需要分号。只有在编写 cfscript 语法时才需要这些。

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

Description

Appends an array element to an array. Concatenates arrays when the merge argument is set to true and the value argument is an array.

Returns

True, on successful completion.

Category

Array functions

Function syntax

ArrayAppend(array, value [,merge])

来自ArrayAppend documentation.