2012-04-23 38 views
1

我仍然試圖讓我在Coldfusion上的握手...如何使用Coldfusion cfdirectory和randRange函數輸出隨機文件?

我需要創建一個直接的文件(說有10個文件),並輸出5個隨機文件。獲取和輸出文件是好的,但我不知道在哪裏適合randrange。這裏是我的代碼:

<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張的隨機圖片。

感謝您的一些見解!

編輯:
這是我最後只是 - 一個問題沒有解決 -

<!-- 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)告訴我。任何想法我做錯了什麼?

+0

你想用'ArrayDeleteAt()'還引用您的ArrayDeleteAt變量時不需要輸出跡象。 ''cfset ArrayDeleteAt(dirArr,dirArr [pos])>'除非您直接輸出變量,否則幾乎不需要使用#。 – 2012-04-23 11:54:32

+0

hm。那個錯誤:「值C:\ path \ logos \ marke1111111111114_test.png.png不能被轉換爲一個數字。」 – frequent 2012-04-23 11:57:00

+1

關於代碼風格的說明。只有當你需要圍繞##中的變量或函數時,纔打算輸出結果。 是不必要的。 更具可讀性。函數的參數也是如此myfunc(#varname#)是不好的做法,myfunc(varname)更可取 – 2012-04-23 12:03:19

回答

1

我不確定您的代碼是否會實際工作,因爲它似乎有幾個語法錯誤。您還正在對IMG一個目錄列表,但隨後從標誌拉的圖像和你沒有講明是什麼關係,這些目錄之間預留

這些問題,這是我將如何處理這個。

<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個元素,然後可以將其輸出爲列表。

作爲一個附註,不建議重複使用<cfimage>和相關函數。如果您需要調整大小或操作圖像,則應將其緩存回磁盤,而不是每次請求重複操作。

+0

非常感謝,你說得對,我的錯誤現在清理了,然後按照你的建議進行嘗試 – frequent 2012-04-23 09:19:49

+0

明白了,見上面我在ARRAYDELETE中還有一個問題是未定義的,不是我希望它是一個變量:-)任何想法? – frequent 2012-04-23 11:03:56

+0

什麼是確切的錯誤信息? – 2012-04-23 11:53:35

3

一個簡單的辦法是一次洗牌的數組,然後取前五個項目:

<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# /> 
+0

我喜歡開箱即用的解決方案:-) – frequent 2012-04-23 18:13:02