Jack Pilowsky
Jack Pilowsky

Reputation: 2303

Why CFML keeps trying to assign rather than outputting my variable?

I'm trying to convert a list of database column names and check it inside of a query. So for example, this list:

<cfset permissionsLevelList = 'blnGalleryAdd,blnGalleryUpdate,blnGalleryDelete,blnGalleryMaster'>

This is NOT being passed on through the UI in any way.

So, then I convert it to an array and try to loop through it in my query

<cfset permissionsLevelArray = listToArray(permissionsLevelList)>
AND (
    <cfloop array="#permissionsLevelArray#" index="i">
        ap.#permissionsLevelArray[i]# = 1 
        <cfif i neq arrayLen(permissionsLevelArray)>OR</cfif>
    </cfloop>
)

But I'm getting the error cannot

cast [blnGalleryAdd] string to a number value

So, the compiler is trying to assign it as variable when I just want to output it.

Does anyone know any good workaround for this?

Upvotes: 2

Views: 91

Answers (3)

Dan Bracuk
Dan Bracuk

Reputation: 20804

This documentation, tells me that when looping through an array, the index attribute represents the array element, not the position of that element. Your error message reinforced that.

All you have to do is change this:

<cfloop array="#permissionsLevelArray#" index="i">
    ap.#permissionsLevelArray[i]# = 1 

to this

<cfloop array="#permissionsLevelArray#" index="i">
    ap.#i# = 1 

Not related to your question, but you don't really need this:

<cfif i neq arrayLen(permissionsLevelArray)>OR</cfif>

Since you are using or logic, you can start or finish that block with an inequality and put the word or inside the loop on every iteration. Something like this:

and
(1 = 2
<cfloop>
or
this = that
</cfloop>
)

Upvotes: 7

MT0
MT0

Reputation: 168051

AND (
  <cfscript>
  permissionsLevelArray = listToArray(permissionsLevelList);
  for ( i = 1; i LTE ArrayLen( permissionsLevelArray ); i++ )
  {
    WriteOutput( (i>1?" OR ":"") & "ap." & permissionsLevelArray[i] & " = 1" ); 
  }
  </cfscript>
)

Upvotes: 1

Jack Pilowsky
Jack Pilowsky

Reputation: 2303

This ended up working:

<cfset realIndex = 1>
<cfloop array="#permissionsLevelArray#" index="i">
    ap.#i# #chr(61)# 1
    <cfif realIndex neq arrayLen(permissionsLevelArray)>OR</cfif>
    <cfset realIndex++>
</cfloop>

Upvotes: 1

Related Questions