Coldfusion limit cfloop to 10

1.9k views Asked by At

I'm working with quite a large array of items in a cfloop. I would like to pare this down and add pagination. Is there anyway in my cfloop to limit the array to the first 10?

I have

<cfloop array="#qryItems#" index="index">

I have tried turning it into a condition loop with no luck and a few other things. I haven't touched coldfusion in a while and am a little rusty. Google is not helping haha

I have tried

<cfloop from="1" to="10" array="#qryItems#" index="index">

and have also tried max_rows

 <cfloop maxrows="10" array="#qryItems#" index="index">

each time I get the error message

"Attribute validation error for tag CFLOOP."

2

There are 2 answers

2
Twillen On

There is no combination of attributes for cfloop to accomplish what your expecting. As BKBK suggested, you'll need to use a from/to loop to output a select group of records. If I understand your requirements correctly, I would update your cfloop with a new index variable as well, and then set the old variable by referencing the array element.

The two cfloops below output the same data, with the second displaying only the records in the pagination range.

<cfset qryItems = [1,2,3,4,5,6,7,8,9,10,'a','b','c','d'] />
<cfoutput>
    <!--- Current loop: Outputs all records --->
    <cfloop array="#qryItems#" index="index">
        #index#
    </cfloop>
    <cfset paginationStart = 1 />
    <cfset paginationEnd = 10 />
    <!--- Only the range of of records requested --->
    <cfloop from="#paginationStart#" to="#paginationEnd#" index="indexNumber">
        <cfset index = qryItems[indexNumber] />
        <!--- code remain the same --->
        #index#
    </cfloop>
</cfoutput>
6
BKBK On
<cfloop from="1" to="10" index="index">
    <!--- Then do your business with array elements qryItems[index], that is, with qryItems[1], qryItems[2],..., qryItems[10] --->
</cfloop>