2

I am a beginner in ColdFusion and want to append values to an array from within a loop. I have written this code, but it does not work for me.

<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>
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
vinny
  • 128
  • 1
  • 12
  • The documentation on arrayAppend will have an example showing how to use it properly. Having said that, given that array functions work on cfquery columns, you might not need another variable. Finally, running a query inside a loop from another query is almost always a very bad idea. If the datasource is the same, look at ways to get your data with one query only. – Dan Bracuk Mar 07 '17 at 12:00
  • i have read documentation also reading some article.can you help me how to write and use as list ?. – vinny Mar 07 '17 at 13:21
  • List var use with where clause with another query – vinny Mar 07 '17 at 13:24

1 Answers1

3

You did not describe how the code is not working for you but my guess is on this line.

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

You are setting the return value of the ArrayAppend() function call to your array variable myArray but that function returns a boolean on the success or failure. So your array is being overwritten with the boolean return value from the call. It seems like you just need to change it to something like:

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

Also notice that the pound signs # are not needed when the variable is used as part of a function call like this.

And the semicolon is not need when using tag syntax like this. Those are only needed when writing cfscript syntax.

Some ColdFusion functions work they way you had tried but others do not. This is why you need to read the documentation about a function when trying to use it.

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

From the ArrayAppend documentation.

Miguel-F
  • 13,450
  • 6
  • 38
  • 63
  • 1
    I really appreciate what you told me when I have not explained properly problem in spite of giving me suitable solution all the help you gave me ...thanks – vinny Mar 08 '17 at 04:34