首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用cfdirectory和randRange函数输出随机文件?

如何使用cfdirectory和randRange函数输出随机文件?
EN

Stack Overflow用户
提问于 2012-04-23 07:46:50
回答 2查看 1K关注 0票数 1

我还在试着抓住感冒.

我需要创建一个直接的文件(例如,有10个文件),并输出5个随机文件。取得和输出文件是可以的,但我不知道哪里适合随机范围。这是我的代码:

代码语言:javascript
复制
 <cfdirectory action="list" directory="#expandpath("img/")#" filter="some*.*" name="dir">
     <!--- imgID --->
     <CFSET imgID= #RandRange(1, #dir.allRecords#)#>
     <!--- this only grabs the first 5 files --->
     <cfoutput query="dir" maxrows="5">
        <cfif FileExists("#expandpath("img/#name#")#")>
        <cfimage source="#expandpath("img/#name#")#" name="myImage">                                                   <cfif IsImage(myImage) is true>
          <cfset ImageSetAntialiasing(myImage,"on")>
              <cfset ImageScaleToFit(myImage,"highestQuality")>
              <!--- append to a list --->
          <li><cfimage source="#myImage#" action="writeToBrowser"></li>
           </cfif>               
        </cfif>
      </cfoutput>   

这在显示前5张图像时工作正常。不过,我想有5个随机图像。

谢谢你的见解!

编辑:

我就是这样做的-有一个问题没有解决-

代码语言:javascript
复制
<!-- get the directy, listinfo="name" because I only need filenames --->
<cfdirectory action="list" LISTINFO="name" directory="#expandpath(" logos/")#" filter="marke*.*" name="dir">

 <cfset images=[ ]>
 <!-- since dir is not indexable, like dir[pos], I need another array!-->
 <cfset dirArr=[ ]>
 <cfset blocker="false">
 <cfset maxLogos=5>
 <!-- fill new dirArr(ay) -->               
 <cfoutput query="dir">
    <cfset #ArrayAppend(dirArr, #expandpath( "logos/#name#")#)#>
 </cfoutput>
 <!-- loop -->
 <cfloop condition="blocker eq false">
    <-- random position -->
    <cfset pos=R andRange(1, #dir.recordcount#)>
    <cfif #dir.recordcount# eq 0 OR #ArrayLen(images)# gte #maxLogos#>
        <-- STOP loop -->
        <cfset blocker="true">
    </cfif>
    <cfset ArrayAppend(images, #dirArr[pos]#)>
    <!-- BROKEN unknown ARRAYDELETE --> 
    <!--- <cfset ArrayDelete(dirArr, #dirArr[pos]#)> --->
    <!-- IMG -->
    <cfimage source="#dirArr[pos]#" name="myImage">
    <cfif IsImage(myImage) is true>
        <cfoutput>
        <li data-icon="false">
           <cfimage source="#myImage#" action="writeToBrowser">
        </li>
        </cfoutput>
    </cfif>
 </cfloop>

问题是ArrayDelete不工作变量ARRAYDELETE是未定义的,Coldfusion(8)告诉我。知道我做错什么了吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-23 08:59:58

我不确定您的代码是否真的能工作,因为其中似乎有几个语法错误。另外,您正在img上做目录列表,但随后从徽标中提取图像,您还没有清楚地说明这些目录之间的关系。

撇开这些问题不说,我将如何处理这件事。

代码语言:javascript
复制
<cfscript>
// this code is untested, but should get you going
// get list of image file names as an array
dir = directoryList(expandPath("imgs"), false, "name", "*.jpg");
images = [];
while(true) {
  // if out directory list is now empty or we have 5 results, we're done
  if(!arrayLen(dir) or arrayLen(images) gte 5) break;
  // get an image from a random point in the list
  pos = randrange(1, arrayLen(dir));
  // append it to our images array
  arrayAppend(images, dir[pos]);
  // delete form the source array, this avoids duplicates in further iterations
  arrayDeleteAt(dir, pos);
}
</cfscript>

这将为您提供一个图像数组,其中包含0到5个元素,然后您可以将其作为一个列表输出。

附带说明,不宜重复使用和相关功能。如果需要调整图像的大小或操作,则应将其缓存回磁盘,而不是重复每个请求的操作。

票数 1
EN

Stack Overflow用户

发布于 2012-04-23 17:24:51

一个简单的替代方法是对数组进行一次洗牌,然后取前五项:

代码语言:javascript
复制
<cfset MaxLogos = 5 />
<cfset Images   = [] />
<cfset Files    = DirectoryList( expandPath("logos") , false, "name" , "marke*.jpg" ) />

<cfset createObject( "java", "java.util.Collections" ).shuffle( Files ) />

<cfloop index="i" from="1" to=#Min(MaxLogos,ArrayLen(Files))# >
    <cfset ArrayAppend( Images , Files[i] ) />
</cfloop>

<cfdump var=#Images# />
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10276707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档