Copy link to clipboard
Copied
I've never done calculations in ColdFusion before so I don't even know where to start. I would like to put a total at the bottom of each row of returned values. How do I go about doing that? I know none of this right, but here's my code:
<cfoutput query="GetList">
<tr>
<td valign="top" id="text">#ProjectName#</td>
<td valign="top" id="text">#Phase#</td>
<td valign="top" id="text">#Target_Quarter#</td>
<td valign="top" id="text">#DollarFormat(Project_Cost)#</td>
<td valign="top" id="text">#DollarFormat(Q1_Costs)#</td>
<td valign="top" id="text">#DollarFormat(Actual_q1_Costs)#</td>
</tr>
</cfoutput>
Heres where I want to show the totals from the above values:
<cfoutput query="GetTotal">
<tr>
<td valign="top" id="text" align="right" colspan="3"><b>TOTALS:</b></td>
<td valign="top" id="text2">#DollarFormat(Total.Project_Cost)#</td>
<td valign="top" id="text2">#DollarFormat(Total.Q1_Costs)#</td>
<td valign="top" id="text2">#DollarFormat(Total.Actual_q1_Costs)#</td>
</tr>
</cfoutput>
Thanks!
Just like any variable, you have to define it before you can use it.
Inorder to be able to use the variable 'TotalProjectCost' on the left side of this assignement.
<cfset totalProjectCost = totalProjectCost + Project_Cost>
You must define it.
I.E. at the very beginning of your loop.
<cfset totalProjectCost = 0>
...
<cfloop query="GetList">
Basic programmer 101 stuff.
Copy link to clipboard
Copied
Rather then a separate query you can use arraySum() on a query column to total up the values in your query. Try this:
<cfoutput query="GetList">
<tr>
<td valign="top" id="text">#ProjectName#</td>
<td valign="top" id="text">#Phase#</td>
<td valign="top" id="text">#Target_Quarter#</
<cfoutput>
</cfoutput>
Copy link to clipboard
Copied
<Bryan> wrote:
I've never done calculations in ColdFusion before so I don't even know where to start. I would like to put a total at the bottom of each row of returned values. How do I go about doing that? I know none of this right, but here's my code:
<cfoutput query="GetList">
<tr>
<td valign="top" id="text">#ProjectName#</td>
<td valign="top" id="text">#Phase#</td>
<td valign="top" id="text">#Target_Quarter#</td>
<td valign="top" id="text">#DollarFormat(Project_Cost)#</td>
<td valign="top" id="text">#DollarFormat(Q1_Costs)#</td>
<td valign="top" id="text">#DollarFormat(Actual_q1_Costs)#</td></tr>
</cfoutput>
As you are looping over the record set, record the total of each itme.
</tr>
<cfset totalProjectCost = totalProjectCost + Project_Cost>
etc.
</cfoutput>
Then in the last row, just output these totals.
<cfoutput>
<tr>
....
<td>#DollarFormat(totalProjectCost)#</td>
...
</tr>
</cfoutput>
Copy link to clipboard
Copied
ilsaac - Tried your suggest but got this error: Variable TOTALPROJECTCOST is undefined.
Is this how the code should look:
<cfoutput query="GetList">
<tr>
<td valign="top" id="text">#ProjectName#</td>
<td valign="top" id="text">#Phase#</td>
<td valign="top" id="text">#Target_Quarter#</td>
<td valign="top" id="text">#DollarFormat(Project_Cost)#</td>
<td valign="top" id="text">#DollarFormat(Q1_Costs)#</td>
<td valign="top" id="text">#DollarFormat(Actual_q1_Costs)#</td>
</tr>
<cfset totalProjectCost = totalProjectCost + Project_Cost>
<cfset totalQ1_Costs = totalQ1_Costs + Q1_Costs>
<cfset totalActual_q1_Costs = totalActual_q1_Costs + Actual_q1_Costs>
</cfoutput>
<cfoutput>
<tr>
<td valign="top" id="text" align="right" colspan="3"><b>TOTALS:</b></td>
<td valign="top" id="text2">#DollarFormat(totalProjectCost)#</td>
<td valign="top" id="text2">#DollarFormat(totalQ1_Costs)#</td>
<td valign="top" id="text2">#DollarFormat(totalActual_q1_Costs)#</td>
</tr>
</cfoutput>
Copy link to clipboard
Copied
That's because they don't exist the first time thru the loop. Declare the vars first outside the loop. Or use arraySum() like I suggested.
Copy link to clipboard
Copied
Also a valid option.
Copy link to clipboard
Copied
Just like any variable, you have to define it before you can use it.
Inorder to be able to use the variable 'TotalProjectCost' on the left side of this assignement.
<cfset totalProjectCost = totalProjectCost + Project_Cost>
You must define it.
I.E. at the very beginning of your loop.
<cfset totalProjectCost = 0>
...
<cfloop query="GetList">
Basic programmer 101 stuff.
Copy link to clipboard
Copied
Thanks for your help guys, and your patience. Sorry for coming off ignorant to basic Programming 101 skills, but that's probably because I am. I'm a graphic artist and only work with ColdFusion once a year to do lists and update forms. I've never used CFLOOP or ARRAYS, so unfortunately you'll need to elaborate more completely in order for me to follow your logic
Copy link to clipboard
Copied
I'm sorry - but this forum isn't really the place for you then. We've given you good suggestions - hell we've even written the code for you. I'd suggest reading the documenation or taking a class if you're still stuck.
Copy link to clipboard
Copied
ilssac wrote:
Inorder to be able to use the variable 'TotalProjectCost' on the left side of this assignement.
<cfset totalProjectCost = totalProjectCost + Project_Cost>
And if I could tell my left hand from my right hand, I would have properly said "...use the variable 'TotalProjectCost' on the right side...". It was the undefined varaible in the expression 'totalProjectCost + Project_Cost' that was throwing the exception. Because if totalProjectCost is undefined what the heck can ColdFusion add to the value of Project_Cost.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more