Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    308
    Thanks
    0
    Thanked 0 Times in 0 Posts

    random image script only showing one image

    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.

    Code:
    <%
    
    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 / >

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    308
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Disregard, I found a script that works:

    Code:
    <!--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
    
    %>
    Last edited by AshleyQuick; 09-02-2004 at 03:34 PM. Reason: Resolved

  • #3
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,062
    Thanks
    4
    Thanked 8 Times in 8 Posts
    In your original script you did not use the randomize statement. Without it you will always get the same number returned.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    308
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ugh! Good catch.

    Thanks,
    Ash


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •