To read the article online, visit http://www.4GuysFromRolla.com/webtech/082099-1.shtml

Displaying Pictures Randomly

By Ronnie Tantriady


Have you ever got bored looking at same picture everytime you visit a particular web page? A friend of mine who owns a travel agency was worried if regular visitors of his website would be bored looking at the same static picture. On the other hand, he has already collected about 950 scenic pictures of world cities in both JPEG and GIF format. So how do we help him make a web page that will display one of those pictures randomly everytime a visit is made to his ASP enabled website?

Simple. First, I created a new directory in his web server and copied all of the JPEG and GIF pictures into it. For obvious reasons I named the directory /pictures.

Next, I created the following ASP file, and saved it as RandDisp.ASP

<% dim x(1000) 'The directory of all my pictures... Const mypath="/pictures" Set filesystem = CreateObject("Scripting.FileSystemObject") Set folder = filesystem.GetFolder(server.mappath(mypath)) Set filecollection = folder.Files 'Step through the files list, keeping track of 'the number of files.... idx=0 For Each file in filecollection idx=idx+1 x(idx)=file.name Next 'Choose a random picture randomize timer whichNo=int(rnd()*idx)+1 'Clean up... set filesystem=nothing set folder=nothing set filecollection=nothing 'Display the image! response.write "<img height=97% src=" & mypath & "/" response.write x(whichNO)& " alt=" & x(whichNo) & " border=5>" %>

Let's take a look at the code.

Dim x(1000)

We created an array of size 1000, which is more than enough to store the file names of those 950 JPEG and GIF files.

mypath="/pictures" Set filesystem = CreateObject("Scripting.FileSystemObject") Set folder = filesystem.GetFolder(server.mappath(mypath)) Set filecollection = folder.Files

These lines instruct the server to grab the filenames of all files resided in the folder called /pictures. (If you are unfamiliar with the FileSystemObject be sure to read Reading/Writing Text Files using ASP.)

idx=0 For Each file in filecollection idx=idx+1 x(idx)=file.name Next

Transfer each and every file name to the array we defined above, and at the same time count the number of actual files found in the /pictures directory.

randomize timer whichNo=int(rnd()*idx)+1

Generate a random number between 1 to the number that matches the actual number of files found in the /pictures directory. Using the generated random number as the index of the array cell, store the content of that particular array cell to a variable named whichNo.

set filesystem=nothing set folder=nothing set filecollection=nothing

Close the file system objects.

response.write "<img height=97% src=" & mypath & "/" response.write x(whichNO)& " alt=" & x(whichNo) & " border=5>"

Generate an HTML line to display the picture at 97% the height of the browser window.

Amazingly simple, isn't it?

Happy Programming!


Attachments:

  • View the Code for RandDisp.asp in text format


  • Article Information
    Article Title: Displaying Pictures Randomly
    Article Author: Ronnie Tantriady
    Published Date: Friday, August 20, 1999
    Article URL: http://www.4GuysFromRolla.com/webtech/082099-1.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers