...

View Full Version : random image script only showing one image



AshleyQuick
09-02-2004, 04:13 PM
Initially, I had a folder in the fleet directory so i thought that might be the culprit. I removed that folder and the images still are not rotating...it displays one particular image every time.


<%

Const IMGS_DIR = "/art/fleet/"

Dim objFSO, objFolderObject, objFileCollection, objFile

Dim intFileNumberToUse, intFileLooper

Dim objImageFileToUse

Dim strImageSrcText

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolderObject = objFSO.GetFolder(Server.MapPath(IMGS_DIR))
Set objFSO = Nothing

Set objFileCollection = objFolderObject.Files
Set objFolderObject = Nothing

intFileNumberToUse = Int(objFileCollection.Count * Rnd) + 1

intFileLooper = 1
For Each objFile in objFileCollection
If intFileLooper = intFileNumberToUse Then
Set objImageFileToUse = objFile
Exit For
End If
intFileLooper = intFileLooper + 1
Next

Set objFileCollection = Nothing

strImageSrcText = IMGS_DIR & objImageFileToUse.Name

Set objImageFileToUse = Nothing

%>
<img
align = "right"
src = "<%= strImageSrcText %>"
width = "473"
height = "213"
alt = "Random Image"
/>
<br / >

AshleyQuick
09-02-2004, 04:33 PM
Disregard, I found a script that works:


<!--METADATA TYPE="typelib"
UUID="00000205-0000-0010-8000-00AA006D2EA4"
NAME="ADODB Type Library"
-->
<%

'#AUTHOR:
'# http://www.davelozinski.com/scripts

Const IMAGE_DIRECTORY = "/art/fleet/"

Randomize
Dim imageFiles
Dim tempString
tempString = ""
Dim RandomNumber
Dim File, Folder, fso
Set fso = Server.CreateObject("Scripting.FileSystemObject")

If (fso.FolderExists(Server.MapPath(IMAGE_DIRECTORY))) Then
Set Folder = fso.GetFolder(Server.MapPath(IMAGE_DIRECTORY))
For Each File In Folder.Files
tempString = tempString & File.Name & ","
Next
tempString = tempString & ","
tempString = StrReverse(tempString)
tempString = Replace(tempString, ",,", "", 1)
tempString = StrReverse(tempString)
imageFiles = Split(tempString, ",")
RandomNumber = Cint((UBound(imageFiles) - LBound(imageFiles)) * Rnd + LBound(imageFiles))
Set File = Server.CreateObject("ADODB.Stream")
File.Type = adTypeBinary
File.Open
File.LoadFromFile (Server.MapPath(IMAGE_DIRECTORY & imageFiles(RandomNumber)))
If (CBool(InStr(imageFiles(RandomNumber),".gif"))) Then
Response.ContentType = "image/gif"
ElseIf (CBool(InStr(imageFiles(RandomNumber),".jpg"))) Then
Response.ContentType = "image/jpg"
ElseIf (CBool(InStr(imageFiles(RandomNumber),".jpeg"))) Then
Response.ContentType = "image/jpeg"
End If
Response.BinaryWrite File.Read
File.Close
End If
Set File = Nothing
Set Folder = Nothing
Set fso = Nothing

%>

miranda
09-05-2004, 05:21 PM
In your original script you did not use the randomize statement. Without it you will always get the same number returned.

AshleyQuick
09-06-2004, 06:12 AM
Ugh! Good catch.

Thanks,
Ash



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum