4

I'm stuck... can't remember how to make this work. The code:

<cfquery name = "getsomething>
   select a, b, c
   from d
   where id = '1'
</cfquery>
<cfloop collection="#arguments#" item="argument">
    <cfif #StructFind(arguments, argument)# neq #getsomething.argument[0]#><!--- here's the problem --->
        do something
    </cfif>
</cfloop>

The query returns one record; I need to get the values of each column for that record. The column name is a variable (argument). What syntax do I need to replace

 #getsomething.argument[0]#

? Thanks.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
earachefl
  • 1,880
  • 7
  • 31
  • 55

1 Answers1

7

You need to make a couple of adjustments:

I see you are looping using the "collection" argument. This implies you have a data structure like so:

<cfset arguments = StructNew() />
<cfset arguments.a = 'x' />
<cfset arguments.b = 'y' />
<cfset arguments.c = 'c' />

You can see that the values do not matter in this case--what matters is that by using the "collection" argument, you are working with a struct. Somewhat incorrect, but let's move forward with the assumption.

You do not want the value of your arguments dynamically evaluated, you want the keys--they map to your columns, so loop like this:

<cfloop list="#StructKeyList(arguments)#" index="argument">

then, the following code works:

<cfif StructFind(arguments, argument) neq getsomething[argument][1]>

Note that in this answer, I've changed your query index from 0 to 1: cfquery arrays are 1-based, so the first row is not [0], but [1].

Shawn Holmes
  • 3,752
  • 22
  • 25
  • Perfect, thank you. I'd actually tried that earlier and got a "table is not indexable by FIELDNAMES" error which threw me off - I'd forgotten that arguments.fieldnames existed. – earachefl Oct 05 '11 at 15:22
  • 1
    @earachefl - FYI, either a `collection` or `list` loop would work. The key to making things work is using array notation for your query as Shawn demonstrated. (Though you might want to verify the query is not empty to avoid a different error.) The "not indexable ..." message is usually a cryptic way of saying "that column name does not exist in your query". – Leigh Oct 05 '11 at 16:28
  • 1
    Very odd to see `StructFind` used here. Simpler to do: `` ? – Peter Boughton Oct 05 '11 at 17:29
  • 1
    Also, since additional arguments can potentially be passed in, meaning unknown columns would attempt to be checked, looping through `list=#QueryName.ColumnList#` instead probably makes more sense. – Peter Boughton Oct 05 '11 at 17:33