8

I am looping through the results of a query, and I need to limit the number of rows displayed. I need to use cfoutput because I'm using the group attribute, and I cannot use the maxrows because not all rows will be displayed.

I tried to use <cfbreak> inside the <cfoutput>, but that throws an error.

How can I break out of the <cfoutput> loop?

Yisroel
  • 8,164
  • 4
  • 26
  • 26
  • 2
    http://cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/main.html#bugId=72819 – Henry Oct 18 '11 at 00:56
  • 4
    `cfoutput` is a bit like Hotel Calfornia. AFAIK, there is no elegant way to break out of it early. If you are using a database like MS SQL 2005, I would suggest limiting the rows in SQL, not cfoutput. – Leigh Oct 18 '11 at 02:17
  • Can you not "group by" in your query to eliminate the dupes and then just use cfloop, from which you can cfbreak? – Barry Jordan Oct 18 '11 at 07:20
  • I'm not using the group to remove duplicates, i'm using it to group them. but i only show the group if there is more than one row in it. – Yisroel Oct 18 '11 at 20:06
  • @Yisroel - Re: *"if there is more than one row in it."* Why not just filter it out in your sql? Also, which database are you using? – Leigh Oct 18 '11 at 21:20
  • The fact that there's no simple answer for this sums up Cold Fusion quite well. – Tastybrownies Jul 03 '17 at 03:17

2 Answers2

8

If your group by is only there to remove duplicates from your results I would suggest using your query to cut them down then you can cfloop (select distinct and reducing your returned column list).

If you are using your group by to "group" your results You could run a counter within your loop and a cfif statement inside your first loop to omit later results.

You could fake the group by option in your cfloop by matching value from previous row if you need cfbreak

<cfloop query="queryname">
  <cfif queryname.column[currentrow-1] neq queryname.column[currentrow]>
    #queryname.column#
  </cfif>
</cfloop>

Random note: you can maxrows on any/all levels of your grouped cfoutput

<cfset tmp = querynew('id,dd')>
<cfloop from="1" to="20" index="i">
  <cfset queryaddrow(tmp,1)>
  <cfset querysetcell(tmp,'id',rand(),i)>
  <cfset querysetcell(tmp,'dd',(i mod 4),i)>
</cfloop>
<cfquery dbtype="query" name="tmp">select * from tmp order by dd</cfquery>

<cfoutput query="tmp" group="dd" maxrows="2">#dd#<br
  <ul>
    <cfoutput maxrows="2" group="id"><li>#id#</li></cfoutput>
  </ul>
</cfoutput>
ale
  • 6,369
  • 7
  • 55
  • 65
4

You could use the cfthrow tag to trigger an exception that will allow you to break out of the loop using cfcatch you can then ignore the exception and continue processing. That will give you what you want.

    <cftry>
    <cfset i = 0>
    <cfoutput query="qMyQuery" group="someGroup">
            <cfset i = i + 1>
            Parent
                    <cfoutput>
                            Child
                    </cfoutput>

                    <cfif i GTE 10>
                            <cfthrow type="break">
                    </cfif>
    </cfoutput>

    <cfcatch type="break">
            <!--- DO NOTHING - THIS IS A HACK FOR NOT BEING ABLE TO USE CFBREAK inside cfoutput. --->
    </cfcatch>
    </cftry>
M.Scherzer
  • 921
  • 7
  • 9