Use SQL to do this:

SELECT
    category
    ,COUNT(category) AS categoryCount
FROM
    MyTable
GROUP BY
    category

m!ke 

-----Original Message-----
From: daniel kessler [mailto:[EMAIL PROTECTED] 
Sent: Friday, September 26, 2008 12:31 PM
To: cf-talk
Subject: List and count from a database

I am querying a db and it worked fine.  The results are of a single
field, category, and they're in ascending order

I want to loop through the results, count up how many of each item are
repeated, then put out the item and the count.  So the record set
currently looks like this:

Health in the News
Health in the News
What are you doing?


I want to output that like:
Health in the News (2)
What are you doing? (1)


I feel that I have it basically, but the bottom part (the last CFIF)
feels like a work-around.  I want to know a better way of doing this.

<cfset the_num = 0>
<cfloop from="1" to="#features.recordCount#" index="fe">
        <cfparam name = "the_category" type = "string" default =
"#features.category[1]#">
        <cfif the_category EQ features.category[fe]>
                  <cfset the_num = the_num+1>
         <cfelse>
                  <p>#features.category[fe-1]# (#the_num#)</p>
                  <cfset the_num = 0>
                  <cfset the_category = features.category[fe]>
         </cfif>
</cfloop>
<cfif the_category NEQ features.category[1]>
         <p>#the_category# (#the_num+1#)</p> </cfif>


thanks in advance for any assistance.


daniel 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to 
date
Get the Free Trial
http://ad.doubleclick.net/clk;207172674;29440083;f

Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:313128
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to