Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2042
  • Last Modified:

limit cfloop output to 5 rows

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
bede123
Asked:
bede123
  • 4
2 Solutions
 
_agx_Commented:
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
 
js_vaughanCommented:

<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
 
_agx_Commented:
>> <cfset loopLength = Min(ListLen(client.recentlyViewed), 5)>

Excellent coding :)
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
_agx_Commented:
@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
 
bede123Author Commented:
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
 
_agx_Commented:
The points are more than fair IMO.  Thanks @bede123 :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now