Question: Coldfusion limit cfloop to 10

Question

Coldfusion limit cfloop to 10

Answers 2
Added at 2016-12-27 19:12
Tags
Question

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."

Answers
nr: #1 dodano: 2016-12-27 20:12
<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>
nr: #2 dodano: 2016-12-27 23:12

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>
Source Show
◀ Wstecz