Skip to main content
Inspiring
October 29, 2008
Answered

Count structure values

  • October 29, 2008
  • 2 replies
  • 529 views
Right now i count my structure (named "answer") values by:

<cfset answers_sum = answer.response.sum1 + answer.response.sum2 + answer.response.sum3 + answer.response.sum4 + answer.response.sum5 + answer.response.sum6 />

Of course that is not the way to do so as i want it in a loop count to always (despite amount of answer fields in the db) be able to correctly calculate the total.

So it must be in a loop like <cfloop index="i" from="1" to="#application.dbquestions#"> but how do i calculate the total?

Thanks
    This topic has been closed for replies.
    Correct answer Newsgroup_User
    Array notation to the rescue.

    <cfset answers_sum = answers_sum + answer["resonse"]["sum" & i]>

    2 replies

    Inspiring
    October 29, 2008
    Thanks! :) (also gave answers_sum a default value of 0 to enable it's usage, might sound logical but just in case someone else need that info)
    Newsgroup_UserCorrect answer
    Inspiring
    October 29, 2008
    Array notation to the rescue.

    <cfset answers_sum = answers_sum + answer["resonse"]["sum" & i]>