Search code examples
coldfusioncoldfusion-9

Coldfusion Addition of variables from queries


I have a query and I am trying to add the individual values that outputs in the end.

This is the query

<cfloop query="score">
  <cfset scorefinal = score * answerweight>
  <cfif getCategories.surveyidfk eq score.surveyidfk and getCategories.categoryidfk eq score.categoryidfk>
    <cfset result = getcategories.cweight * scorefinal>
    <cfdump var="#result#">
  </cfif>
</cfloop>

This code outputs 1.5 2.5 1.3

I need a method that will add these values in another variable that will allow me to show only a single value in the end.

I was thinking to store these values in an array and then add the elements of the array but I am pretty sure that there is a faster and easier way to do it.


Solution

  • I haven't used ColdFusion in a long time, but this should do it:

    <cfset finalScore = 0>
    <cfloop query="score">
        <cfset scorefinal = score * answerweight>
        <cfif getCategories.surveyidfk eq score.surveyidfk and getCategories.categoryidfk eq score.categoryidfk>
            <cfset result = getcategories.cweight * scorefinal>
            <cfset finalScore = finalScore + result>
            <cfdump var="#result#">
        </cfif>
    </cfloop>
    <cfdump var="#finalScore#">