Print Page | Close Window

Random Selection from Database

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Database Discussion
Forum Description: Discussion and chat on database related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=10019
Printed Date: 28 March 2026 at 7:54am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Random Selection from Database
Posted By: zMaestro
Subject: Random Selection from Database
Date Posted: 16 April 2004 at 10:07am

Hi

I use this code to select a random image from a database.

<%
Rand = "SELECT * FROM TableName"
set RandS = server.createobject("adodb.recordset")
RandS.open Rand, Conn, 3, 1

rndMax = CInt(RandS.RecordCount)
RandS.MoveFirst
Randomize Timer
rndNumber = Int(RND * rndMax)
RandS.Move rndNumber

RandHere = RandHere &  "<IMG src=""" & RandS("Image") & """>"
%>

Is there a way i can select 4 random images instead of only one image.

thanks.




Replies:
Posted By: Mart
Date Posted: 16 April 2004 at 10:16am
Try this:

<%
Rand = "SELECT * FROM TableName"
set RandS = server.createobject("adodb.recordset")
RandS.open Rand, Conn, 3, 1

Dim i

For i = 1 To 4
rndMax = CInt(RandS.RecordCount)
RandS.MoveFirst
Randomize Timer
rndNumber = Int(RND * rndMax)
RandS.Move rndNumber

RandHere = RandHere &  "<IMG src=""" & RandS("Image") &
""">"
Next i

%>




Posted By: zMaestro
Date Posted: 16 April 2004 at 10:36am

hmm, i am not familiar with For NExt statment.. it gives me:

Error Type:
Microsoft VBScript compilation (0x800A0401)
Expected end of statement
/Home.asp, line 390, column 5
Next y
----^



Posted By: Mart
Date Posted: 16 April 2004 at 10:40am
delete the y so it is just Next, I wasn't sure if you had to use 'Next [var]' in Classic ASP.


Posted By: zMaestro
Date Posted: 16 April 2004 at 10:41am

Ohh Thanks Mart.. it worked



Posted By: ljamal
Date Posted: 16 April 2004 at 10:57am
If you are using SQL Server the following will produce records in random order

select * from table order by NewID()

-------------
L. Jamal Walton

http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming


Posted By: vinoth
Date Posted: 17 April 2004 at 5:54am

Cool And Great Developers All Here in Asp,

But I am In PHP



-------------
Zeus

http://www.codelance.tk
The Best Way to Help Poor is not Becoming One of them!


Posted By: ljamal
Date Posted: 17 April 2004 at 9:30am
Originally posted by vinoth vinoth wrote:

Cool And Great Developers All Here in Asp,


But I am In PHP


because this is forum for ASP
there are many PHP development forums, go check those out

that's saying this is a great hamburger restaurant but I want a steak.


-------------
L. Jamal Walton

http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming


Posted By: pmormr
Date Posted: 17 April 2004 at 3:22pm

mmmmm... steak



-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: pmormr
Date Posted: 17 April 2004 at 3:23pm
mmmmm... hamburgers

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: dpyers
Date Posted: 17 April 2004 at 5:18pm
I'd gladly pay you Tuesday for a hamburger today.

-------------

Lead me not into temptation... I know the short cut, follow me.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net