Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

9 Random Images

Status
Not open for further replies.

budapestnori

Technical User
Jan 15, 2004
33
HU
Ok,I hope I can explain this. I have database with two fields one is an auto-increment ID and the other field is a string whose value is an image file name, such as w_img_1.jpg, w_img_2.jpg., etc.

When the page loads, I open the recordset and select all records, sorted by ID. Then, I use a rs.recordcount to determine the number of files. Once I have that number, I use that as my uppperbound in a loop to get get 9 random numbers between 1 and upperbound. That is working very well, but here is my problem. What I want, is each value of an array which is sized to 9., ie, nine random numbers, to contain the value of a unique image name. I am sure there are multiple ways to do this, I just need to know the most effecient. I thought of using the values of the record IDs, but these seem to change as they may be 1,2,3,5,38,43,54 of images are continually added and deleted. Here is the code I have so far:

Randomize

DIM cat,random_number(), counter, check,count

cat = Request("category")
Set cnn1 = Server.CreateObject("ADODB.Connection")
openStr = "driver={Microsoft Access Driver (*.mdb)};" & _
"dbq=" & Server.MapPath("database/images.mdb")
cnn1.Open openStr,"",""

sql = "SELECT * FROM gallery WHERE category = '" & cat & "' Order by ID"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, cnn1, 3, 3

count = rs.recordcount

redim random_number(count)

For counter = 1 to count
random_number(counter) = Int(Rnd * count)+1

for check=1 to counter-1
if random_number(check)= random_number(counter) then
counter=counter-1
end if
next
next
%>

<script type=&quot;text/javascript&quot;>
<!--
//Specify image paths and optional link (set link to &quot;&quot; for no link):

var dynimages=new Array()
<%
comma = &quot;,&quot;
x = 0
while not rs.eof
response.write(&quot;dynimages[&quot; & x & &quot;]=[&quot; & chr(34) & &quot;images/gallery/&quot; & rs(&quot;image&quot;) & chr(34) & comma & Chr(34) & Chr(34) & &quot;]&quot; & chr(13))

x = x + 1
rs.movenext()
wend


%>
 

count = rs.recordcount
dim random_number(9)
for counter = 1 to 9
random_number(counter) = Int(Rnd * count)+1
for check=1 to counter-1
if random_number(check)= random_number(counter) then
counter=counter-1
end if
next
next
for counter = 1 to 9
nr=random_number(counter)
rs.Move nr-1 [/code]'move to the record
'display the record
next


________
George, M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top