Hi Geoff, A few quick comments . . .
Firstly, don't worry about performance. If it becomes a problem you can always refactor. All of the approaches you described are reasonably performing (you're not creating arrays of hundreds of objects), so I'd worry more about maintainability and readability. I would load the query into an iterating business object (lets call it a sales report) and then my output would be: <table> <cfset SalesReport.First()> <cfloop condition="NOT #SalesReport.isLast()#"> <tr> <td>#SalesReport.get("monthname")#</td> <td>#SalesReport.get("salevalue")#</td> </tr> <cfset SalesReport.Next()> </cfloop> <tr> <td>Total</td> <td><cfoutput>#SalesReport.get("thetotal")#</cfoutput></td> </tr> </table> Now you've encapsulated getting thetotal, so you can set that any way you want - a loop when loading the query or a separate db call (db access is probably more expensive, but why not for the first cut just use whatever method makes you smile!!!). Note also that the above code is in a view somewhere. Except in very rare cases (user controls and the like) cfc's shouldn't speak HTML or handle output, and a cfc should pretty much never handle both business logic and display. Best Wishes, Peter -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Geoff Parkhurst Sent: Tuesday, July 18, 2006 6:22 AM To: CFCDev@cfczone.org Subject: [CFCDev] Monthly sales + sales total Hi all, Quick (ish) question - what would be a good way to return a recordset and a total from a cfc? Perhaps I should show some code: Old Skool: <cfquery datasource="blah" name="g"> select monthname,sum(salevalue) as salevalue from sales where theyear="2006" group by monthname </cfquery> <cfset thetotal=0> <table> <cfoutput query="g"> <cfset thetotal=thetotal+salevalue> <tr> <td>#monthname#</td> <td>#salevalue#</td> </tr> </cfoutput> <tr> <td>Total</td> <td><cfoutput>#thetotal#</cfoutput></td> </tr> </table> This is bad because I'm doing calculations in my view - mixing presentation with code etc. CFC stylee: <cfinvoke component="whatever" method="whatever" returnvariable="somestruct"> <cfoutput> <table> <cfloop from="1" to="#variables.somestruct.monthlydata.recordcount#" index="i"> <tr> <td>#variables.somestruct.monthlydata.monthname[i]#</td> <td>#variables.somestruct.monthlydata.salevalue[i]#</td> </tr> </cfloop> <tr> <td>Total</td> <td>#variables.somestruct.thetotal#</td> </tr> </table> </cfoutput> So within my cfc, I loop over the query to calculate the total, and return a struct of both the recordset and this total. Is this the right way to go? It looks to me like it'd be slower than the quick and dirty approach since I'm performing two loops. But I like it because I'm not doing any calculations mid-<table>. But would it be better to have 2 functions - getsalesbymonth("2006") (returns a query) and getsalesforyear("2006") (returns a number) and call them separately? (This looks like 2 database calls, which is even worse I suppose.) Any thoughs appreciated - hope I've not opened a massive can of worms... Regards, Geoff ---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to cfcdev@cfczone.org with the words 'unsubscribe cfcdev' as the subject of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com). An archive of the CFCDev list is available at www.mail-archive.com/cfcdev@cfczone.org ---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to cfcdev@cfczone.org with the words 'unsubscribe cfcdev' as the subject of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com). An archive of the CFCDev list is available at www.mail-archive.com/cfcdev@cfczone.org