Solved

limit cfloop output to 5 rows

Posted on 2010-11-23
6
1,913 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 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
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 
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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

691 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