Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Random Selection from Database
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Random Selection from Database

 Post Reply Post Reply Page  12>
Author
zMaestro View Drop Down
Senior Member
Senior Member


Joined: 11 May 2003
Location: Egypt
Status: Offline
Points: 1183
Post Options Post Options   Thanks (0) Thanks(0)   Quote zMaestro Quote  Post ReplyReply Direct Link To This Post Topic: Random Selection from Database
    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.



Edited by zMaestro
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post 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

%>


Back to Top
zMaestro View Drop Down
Senior Member
Senior Member


Joined: 11 May 2003
Location: Egypt
Status: Offline
Points: 1183
Post Options Post Options   Thanks (0) Thanks(0)   Quote zMaestro Quote  Post ReplyReply Direct Link To This Post 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
----^

Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
zMaestro View Drop Down
Senior Member
Senior Member


Joined: 11 May 2003
Location: Egypt
Status: Offline
Points: 1183
Post Options Post Options   Thanks (0) Thanks(0)   Quote zMaestro Quote  Post ReplyReply Direct Link To This Post Posted: 16 April 2004 at 10:41am

Ohh Thanks Mart.. it worked



Edited by zMaestro
Back to Top
ljamal View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 16 April 2003
Status: Offline
Points: 888
Post Options Post Options   Thanks (0) Thanks(0)   Quote ljamal Quote  Post ReplyReply Direct Link To This Post 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()
Back to Top
vinoth View Drop Down
Newbie
Newbie


Joined: 24 April 2003
Location: India
Status: Offline
Points: 26
Post Options Post Options   Thanks (0) Thanks(0)   Quote vinoth Quote  Post ReplyReply Direct Link To This Post Posted: 17 April 2004 at 5:54am

Cool And Great Developers All Here in Asp,

But I am In PHP

Back to Top
ljamal View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 16 April 2003
Status: Offline
Points: 888
Post Options Post Options   Thanks (0) Thanks(0)   Quote ljamal Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.