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>
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.