我仍然試圖讓我在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)告訴我。任何想法我做錯了什麼?
你想用'ArrayDeleteAt()'還引用您的ArrayDeleteAt變量時不需要輸出跡象。 ''cfset ArrayDeleteAt(dirArr,dirArr [pos])>'除非您直接輸出變量,否則幾乎不需要使用#。 – 2012-04-23 11:54:32
hm。那個錯誤:「值C:\ path \ logos \ marke1111111111114_test.png.png不能被轉換爲一個數字。」 – frequent 2012-04-23 11:57:00
關於代碼風格的說明。只有當你需要圍繞##中的變量或函數時,纔打算輸出結果。是不必要的。 更具可讀性。函數的參數也是如此myfunc(#varname#)是不好的做法,myfunc(varname)更可取 –
2012-04-23 12:03:19