Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Break out of Cfoutput

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?

like image 531
Yisroel Avatar asked Oct 18 '11 00:10

Yisroel


2 Answers

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>
like image 129
Patrick Spenceley Avatar answered Dec 17 '22 02:12

Patrick Spenceley


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>
like image 33
M.Scherzer Avatar answered Dec 17 '22 01:12

M.Scherzer