vchris Posted August 7, 2007 Share Posted August 7, 2007 I have a table where I need to calculate the totals and subtotals. The way it is already done is adding the rows in the table straight from the query and then displaying the subtotal/total. Except now I set the isNull value to -1 instead of 0. Anyway I created a UDF where it will take a list as a parameter and add them if they are not -1 and return the answer. If I do something like #calculate_total("1,2,3,4")# it works great. When I try to use variables instead of values that's when it doesn't work. <!--- Function to calculate totals ---><cffunction name="calculate_total" returntype="numeric"> <cfargument name="mynum" type="any" /> <cfset total = 0> <!--- <cfset mynum = "1,2,3,4"> ---> <cfoutput>Listlen = #listlen(mynum)#</cfoutput> <cfloop list="#mynum#" index="i"> <cfset total = total + i> </cfloop> <cfreturn total></cffunction>...#numberformat_en(calculate_total(release.airsta_v, release.airsto_v, release.airfug_v, release.airspi_v, release.airoth_v))#... listlen of mynum is 1 and value -1. Link to comment Share on other sites More sharing options...
Skemcin Posted August 7, 2007 Share Posted August 7, 2007 you might try breaking up your output so that you do not have too many nested functions. you might need an evaluate function call in there also, like this: <cfset vchris="#release.airsta_v#,#release.airsto_v#,#release.airfug_v#,#release.airspi_v#,#release.airoth_v#"><cfset vchris=calculate_total(vchris)><cfset vchris=numberformat_en(vchris)><cfoutput>#vchris#</cfoutput> You might need: <cfset vchris=calculate_total(evaluate(vchris))> in place of the second <cfset> statement. Of course, once you get this worked out, you might make a UDF just for this purpose as well - one UDF can call another - so you don't have to repeat all the work each place/time you want the output.Hope that helps. Link to comment Share on other sites More sharing options...
vchris Posted August 7, 2007 Author Share Posted August 7, 2007 Seems to work pretty well. Thanks!But do you have any idea why my UDF didn't get the full list? Link to comment Share on other sites More sharing options...
Skemcin Posted August 7, 2007 Share Posted August 7, 2007 Well, I'll try to explain.First off, I think you did exactly what I would (and still) do which is make it work with a hard coded value and then say, hey all I gotta do now is substitute my value. Good approach.However, (and this is not bad) if whatever you did is already inside a # sign, then your variable is not gonna really been seen as that if its nested in an function. ColdFusion looks at the out # sign to instantiate the function and then the function is just literally translated since it doesn't know any better. That's when the "evaluate" (or eval if you want to save a few letters typing) function comes into play. It is used (most commonly) as a nested function to basically identify that what it contains is to be processed again by ColdFusion and to not be taken literally. Essentially, (in this case) it says, this is a variable now go get the value.Other uses of "evaluate" might be in a calculation of a variable and a number, like this: #dateformat(evaluate(now() - 3),"mm/dd/yyyy")# Here, ColdFusion needs to be told that "now()" is to be "evaluated" or processed as a variable before it can carry out the dateformat function.Hope that helps to explain it and doesn't confuse you more. Link to comment Share on other sites More sharing options...
vchris Posted August 7, 2007 Author Share Posted August 7, 2007 So you're saying that if I would've used something like #numberformat_en(calculate_total(evaluate(release.airsta_v), evaluate(release.airsto_v), evaluate(release.airfug_v), evaluate(release.airspi_v), evaluate(release.airoth_v)))#, it would've worked? Link to comment Share on other sites More sharing options...
Skemcin Posted August 7, 2007 Share Posted August 7, 2007 So you're saying that if I would've used something like #numberformat_en(calculate_total(evaluate(release.airsta_v), evaluate(release.airsto_v), evaluate(release.airfug_v), evaluate(release.airspi_v), evaluate(release.airoth_v)))#, it would've worked?yes (conceptually) I'd have to actually try it. And to reduce your typing, you could have changed "evaluate" to "eval" Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.