earwig75
asked on
looping with calculations
I have a query in a .cfc that accepts arguments and then returns some values. I am looping over the cfinvoke and sending a bunch of different arguments to the query and displaying the results in a table with some calculations based on those results. I am wondering; do I need to set each variable to zero (0) with a cfparam like I did 2 below? would I set each variable to zero each time in the loop before the group of calculations begins to avoid it from keeping the value from the previous loop? The 'neq 0' was to avoid division by zero but I put the others in there as well. (This is new to me). Below is a small example of what I'm doing in the loop section, thank you!:
<cfloop list="#myList#" index="theNames">
<cfinvoke component="... returnvariable="camebck">
<cfinvokeargument name="theName" value="#theNames#">
</cfinvoke>
<cfoutput>
<cfparam name="something" default="0">
<cfparam name="somethingelse" default="0">
<cfif camebck.datecr neq 0>
<cfset countstayed = #camebck.datecr# - (#camebck.closedneutral# + #camebck.badones#) />
<cfset something = #camebck.closedbad# + #camebck.closedgood# />
<cfif something neq 0>
<cfset somethingelse = #camebck.closedbad# / #something# * 100 />
<cfelse>
<cfset somethingelse = 0 />
</cfif>
<cfset maybebadones = #countstayed# * #somethingelse# / 100 />
<cfset amounts = maybebadones* 204 />
<cfelse>
<cfset countstayed = 0 />
<cfset something = 0 />
<cfset somethingelse = 0 />
<cfset maybebadones = 0 />
<cfset amounts= 0 />
<tr><td>#theName#</td><td>#countstayed#</td>...............
</cfoutput>
</cfloop>
ASKER
If I set each var to zero, would I only have to put the others in a CFIF, if I am dividing, to prevent division by zero? Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<cfparam name="myVar" default="0">
is the same as:
Open in new window
So, each iteration will contain the value of myVar after it has been set in the previous iteration.To ensure the value of myVar is 0 at the beginning of each loop iteration, just replace <cfparam> with <cfset>:
Open in new window