Although this might not exactly answer your question...
If you are using a database like access, mysql or microsoft sql you can
create the query or view ahead of time with the total in it already and
just call the view in your cfc instead of constructing the query in
coldfusion 

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Geoff Parkhurst
Sent: Tuesday, July 18, 2006 3:22 AM
To: [email protected]
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
[email protected] 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/[email protected]



----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to 
[email protected] 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/[email protected]


Reply via email to