Skip to main content
Inspiring
October 30, 2008
Answered

Dynamic <cfif queryname["answer" & i] EQ 1>

  • October 30, 2008
  • 2 replies
  • 552 views
Is it possible to dynamically loop a <cfif queryname["answer" & i] EQ 1>? Well not for me right now :(

I use this code:

(votecheck is a query)

<cfloop index="i" from="1" to="#application.dbquestions#">
<cfif votecheck["answer" & i] EQ 1>
<cfset voteranswer = votecheck["answer" & i]>
</cfif>
</cfloop>

Earlier i learned about array notation, but that seems not to work in this situation....
    This topic has been closed for replies.
    Correct answer Newsgroup_User
    do you want 'i' to represent a row in your query (your query returns N
    rows of column answer), or does your query return only 1 row with N
    columns answer1, answer2, ..., answerN?

    if the former, correct array notation in that case is:
    votecheck['answer']

    if the latter, your query must be returning more than 1 row and that's
    why the array notation does not work - you need to specify a query row.
    this may work in this case:

    <cfoutput query="votecheck">
    <cfloop index="i" from="1" to="#application.dbquestions#">
    <cfif votecheck["answer" & i][currentrow] EQ 1>
    <cfset voteranswer = votecheck["answer" & i][currentrow]>
    </cfif>
    </cfloop>
    </cfoutput>



    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/

    2 replies

    Inspiring
    October 31, 2008
    Great! Somewhat confusing in the beginning, structures, arrays, array notation, dot notation, but as i now see in fact very simple and logical :)
    Newsgroup_UserCorrect answer
    Inspiring
    October 31, 2008
    do you want 'i' to represent a row in your query (your query returns N
    rows of column answer), or does your query return only 1 row with N
    columns answer1, answer2, ..., answerN?

    if the former, correct array notation in that case is:
    votecheck['answer']

    if the latter, your query must be returning more than 1 row and that's
    why the array notation does not work - you need to specify a query row.
    this may work in this case:

    <cfoutput query="votecheck">
    <cfloop index="i" from="1" to="#application.dbquestions#">
    <cfif votecheck["answer" & i][currentrow] EQ 1>
    <cfset voteranswer = votecheck["answer" & i][currentrow]>
    </cfif>
    </cfloop>
    </cfoutput>



    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/