How to find the nested cfoutput recordcount when using group

痞子三分冷 提交于 2019-12-11 03:36:13

问题


Consider the following:

<cfoutput query="resources" group="type">
    <h4>#type#</h4>
       <cfoutput>
          #name#
       </cfoutput>
 </cfoutput>

resources.recordcount would give me the total number of records, but is there an elegant way of finding out the recordcount of the nested data? e.g

<cfoutput query="resources" group="type">
    <h4>#type# (#noofrecords# of #resources.recordcount#)</h4>
       <cfoutput>
          #name#
       </cfoutput>
 </cfoutput>

I could probably do something hacky with loops, but wondered if there was a way of doing it using the cfoutput groups specifically.


回答1:


I am afraid that you would have to do some counting yourself. There is no RecordCount for the nested grouped output, because it is really all the same query, CF is just doing a little formatting for you.




回答2:


You could do an output before to get the count. This would be more efficient than doing a query of queries.

<cfoutput query="resources" group="type">
  <cfset noofrecords= 0>
  <cfoutput>
    <cfset noofrecords++>
  </cfoutput>
  <h4>#type# (#noofrecords# of #resources.recordcount#)</h4>
  <cfoutput>
    #name#
  </cfoutput>
</cfoutput>



回答3:


Another solution is:

<!--- I chose the pipe delimiter | names of things often contain commas,
      otherwise, use any delimiter you like --->
<cfset TypesList = Valuelist(resources.type,"|")>

<cfoutput query="resources">
  <h4>#Type# (#ListValueCount(TypesList,Type,"|")# of #resources.recordcount#)</h4>
  <cfoutput>#name#</cfoutput>
</cfoutput>

While this will also work for other applications, it won't work for everything. It expects the Category count ('Type' here) to be unique (no two types with the same name). A better way to handle this is to count based on an ID rather than a name. You might have, for instance, a category setup like this

Fruits
  Red Ones
    Strawberries
    Raspberries
  Yellow Ones
    Bananas
Vegetables
 Green ones
   Green peppers
 Red Ones
   Red Peppers
   Tomatoes

Based on Type (as a string), the output would say Red Ones (4), Yellow Ones (1), Green Ones (1), Red Ones (4), but in a category-products relational table structure, coutning based on unique ID of the category would retrieve accurate counts.




回答4:


You could get the count in the original query if you wanted an alternative method, but, you will need to test the performance before you go down this route (although using Query of Queries can also have performance issues as there are no indexes).

SELECT f1.id, f1.name, f1.type, f2.typeCount
FROM foo f1
INNER JOIN (
    SELECT COUNT(type) as typeCount, type
    FROM foo
    GROUP BY type 
) f2 on f1.type = f2.type 
ORDER BY f1.type, f1.name

Then in the CF you can do:

<cfoutput query="resources" group="type">
    <h4>#resources.type# (#resources.typeCount# of #resources.recordCount#)</h4>
    <cfoutput>
      #resources.name#
    </cfoutput>
</cfoutput>

As a side note, in CF10 you can also group inside a cfloop with query like so:

<cfloop query="resources" group="type">
    <h4>#resources.type# (#resources.typeCount# of #resources.recordCount#)</h4>
    <cfloop>
      #resources.name#
    </cfloop>
</cfloop>



回答5:


To get the count of the nested data and display it where you say you want it, I would do this:

<cfoutput query = "rescources" group = "type">
query of queries to get the count this type
output the type and the count
<cfoutput>
output nested data
</cfoutput>
</cfoutput>


来源:https://stackoverflow.com/questions/26798463/how-to-find-the-nested-cfoutput-recordcount-when-using-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!