Solved

limit cfloop output to 5 rows

Posted on 2010-11-23
6
1,829 Views
Last Modified: 2012-05-10
hi all,

i'm using this:

<cfloop list="#client.recentlyViewed#" index="ListItem">
<cfset prodID = listFirst(listItem,"|")>
<cfset prodName = listRest(listItem, "|")>

  <cfoutput >
  #prodName# - #prodID# - #ListItem#<br />
 </cfoutput>
 
</cfloop>

to output some data but i only want to output 5 rows of data. i thought maybe adding maxrows="5" might do it but that throws an error.

any know what i need to do?

thanks
0
Comment
Question by:bede123
  • 4
6 Comments
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 100 total points
ID: 34199348
There's no simple way to limit a "list" loop.  Either add a counter and break out of the loop after 5 items

<cfset counter = 1>
<cfloop list="#client.recentlyViewed#" index="ListItem">
   ... output the item ....
  <cfset counter = counter + 1>
  <cfif counter gte 5>
          <cfbreak>
  </cfif>
</cfloop>

.. OR use a from/to loop.  
0
 
LVL 10

Accepted Solution

by:
js_vaughan earned 400 total points
ID: 34199353

<cfset loopLength = Min(ListLen(client.recentlyViewed), 5)>
<cfloop from="1" to="#loopLength#" index="i">
<cfset ListItem = ListGetAt(client.recentlyViewed,i)>
<cfset prodID = listFirst(listItem,"|")>
<cfset prodName = listRest(listItem, "|")>

<cfoutput >
#prodName# - #prodID# - #ListItem#<br />
</cfoutput>
 
</cfloop>

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 34199619
>> <cfset loopLength = Min(ListLen(client.recentlyViewed), 5)>

Excellent coding :)
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 52

Expert Comment

by:_agx_
ID: 34199651
@bede123 - I was just typing up a from/to example when I saw js_vaughan's post.  Both work, but personally I'd use js_vaughan's code. It's simpler.
0
 
LVL 1

Author Comment

by:bede123
ID: 34199702
thanks for the quick reply guys really really helpful

yes i'm now using js_vaughan's code. works perfectly.

hope what ive done with the points seems fare
0
 
LVL 52

Expert Comment

by:_agx_
ID: 34199818
The points are more than fair IMO.  Thanks @bede123 :)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question