?
Solved

limit cfloop output to 5 rows

Posted on 2010-11-23
6
Medium Priority
?
1,955 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 400 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 1600 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
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 
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

Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

Question has a verified solution.

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

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 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