Change the image every refresh...

Is there a way I can have say 5 images and each time you visit the page/refresh it displays a random one? or displys them in an order?

Comments

  • PythonPython Forum Leader The Royal RAM

    The Royal Ram

  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    lol. ok thanks.
  • PythonPython Forum Leader The Royal RAM
    hes after a script where the image changes each time the page loads.


    this sort of script is very useful when it comes to displaying adverisers banners :)

    The Royal Ram

  • CoreyBryantCoreyBryant Beginner Link Clerk
    There are a few different ways - ASP, javascript or as pointed out - PHP.

    For ASP, I use something like:
    [code:1:d33ff9c5b2]
    <%
    Randomize
    '==========================================================
    ' Method #3: This method is the most work for the server,
    ' but it allows you to simply drop a new image into the
    ' image directory specified and it will automatically
    ' start showing it randomly with the others.
    '==========================================================
    ' Set our random images pickup directory. All files in
    ' this direcotory will be randomly displayed so it's
    ' important that the directory contain only image files
    ' that you want to display. Non-image files and files
    ' that shouldn't be displayed should not be placed into
    ' this directory.
    Const IMGS_DIR = "images/rotating/"

    ' Variables for our FileSystemObject objects
    Dim objFSO, objFolderObject, objFileCollection, objFile

    ' A pair of integers for our random image selection
    Dim intFileNumberToUse, intFileLooper

    ' A "handle" to the file we choose to use
    Dim objImageFileToUse

    ' A variable to build our image tag
    Dim strImageSrcText

    ' Lets see what's in the directory:
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    Set objFolderObject = objFSO.GetFolder(Server.MapPath(IMGS_DIR))
    Set objFSO = Nothing

    Set objFileCollection = objFolderObject.Files
    Set objFolderObject = Nothing

    ' Get a count of files and use it to generate a random
    ' number from 1 to the count.
    intFileNumberToUse = Int(objFileCollection.Count * Rnd) + 1

    ' Set up loop control so we exit when we get to the random
    ' file number we just picked.
    intFileLooper = 1
    For Each objFile in objFileCollection
    If intFileLooper = intFileNumberToUse Then
    ' Get a "handle" on the appropriate file
    Set objImageFileToUse = objFile
    Exit For
    End If
    intFileLooper = intFileLooper + 1
    Next

    Set objFileCollection = Nothing

    ' Build our img src tag text
    strImageSrcText = IMGS_DIR & objImageFileToUse.Name

    Set objImageFileToUse = Nothing

    ' Show the image:
    %>

    <p align="center">
    <img
    src = "<%= strImageSrcText %>"
    alt = "Random Image"
    />
    <br / >

    <%
    '==========================================================
    ' Interesting Footnote:
    ' I always used to do things in this order because it
    ' seemed to make sense to me:
    '
    ' Get FSO
    ' Get Folder Using FSO
    ' Get File Collection Using Folder
    ' Get File Using File Collection
    ' Do File Stuff
    ' Release File
    ' Release File Collection
    ' Release Folder
    ' Release FSO
    '
    ' Notice in the code above I do this however:
    '
    ' Get FSO
    ' Get Folder Using FSO
    ' Release FSO
    ' Get File Collection Using Folder
    ' Release Folder
    ' Get File Using File Collection
    ' Release File Collection
    ' Do File Stuff
    ' Release File
    '
    ' Notice the difference... in the first method I have 4
    ' objects allocated at one point while in the second
    ' method I never have more then 2 objects open at once!
    '
    ' It seems trivial, but the easiest way to increase ASP
    ' performance is to get in and out as fast as possible
    ' and this type of code helps accomplish that.
    '==========================================================
    %>
    [/code:1:d33ff9c5b2]
  • PythonPython Forum Leader The Royal RAM
    hmm im guessing you didnt just type that up, hehe

    The Royal Ram

  • CoreyBryantCoreyBryant Beginner Link Clerk
    :) No - I actually use that in one of my websites. I rarely type too much - copy and paste seems to be a lot easier at times
  • PythonPython Forum Leader The Royal RAM
    your right there

    The Royal Ram

Sign In or Register to comment.