When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs



















internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Data Warehouse Architect (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

Published: Friday, August 20, 1999

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?

- continued -

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


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article


  • The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers