Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

limit cfloop output to 5 rows

Posted on 2010-11-23
6
Medium Priority
?
1,997 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 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