1

I' am finding it difficult to understand this. How can I limit the results to 50 only. Let say if in the directory I have 1000 files, how can I limit it so that only 50 files are looped over.

<cfdirectory action="list" directory="#ExpandPath('/downloaded/')#" name="listRoot" filter="*.xml" recurse="false" sort="datelastmodified asc">
<cfoutput>
   <cfloop query="listRoot" from="1" to="50" index="i">
           ....
   </cfloop>
</cfoutput>

When I run the above code I get the following error message

Attribute validation error for tag CFLOOP.

4

2 回答 2

6

如果您查看完整的错误消息,它包含答案(强调我的):

它具有无效的属性组合:from、index、query、to。可能的组合是

  • 必需属性:“查询”。可选属性:'endrow,startrow'
  • ...
  • 必需属性:'from,index,to'。可选属性:'step'

该代码试图混合两种不同类型的循环:查询循环和从/到循环。这不是一个有效的组合。您可以使用query循环循环from/to,但不能同时使用。

话虽如此,由于目标是显示输出,因此确实不需要cfloop. 只需将cfoutput与“startRow”和“maxRows”属性一起使用:

   <cfoutput query="listRoot" startRow="1" maxRows="50">
       #name#<br>
   </cfoutput>

如另一个答案中所述,最新版本的 CF 也支持for ...in循环

<cfscript>
   for (row in listRoot) {
      writeOutput("<br>Debug: name value = "& row.name );
   }
</cfscript>
于 2016-06-13T03:52:18.230 回答
2

您可以通过以下方式访问查询中的特定行:

query[columnName][rowIndex]

为了做 afrom to loop而不是 a each loop,去:

<cfoutput>
    <cfloop from="1" to="50" index="i">
        #listRoot["name"][i]#<br>
    </cfloop>
</cfoutput>
于 2016-06-13T03:37:08.123 回答