开发者

Getting value from cfquery when query column is a variable

I'm stuck... can't remember how 开发者_C百科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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜