Web Wiz - Green Windows Web Hosting

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

Random Select from Database

 Post Reply Post Reply Page  123>
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 Select from Database
    Posted: 17 October 2003 at 7:19am

I have a catalogue containing about 20 items, i list them by:

<%
set RSnav = server.createobject ("ADODB.recordset")
SQLnav = "SELECT * FROM eCatalog;"
RSnav.open SQLnav, Conn, 1, 3

While Not RSnav.EOF
response.write " " & RSnav("ID") & ".<br>"
RSnav.MoveNext
Wend

%>

this code lists all the items in the database, what i want is just to select one item randomly and display it.
so only item like number 5 or item no 9, so as to change the content with every reload.

Thanks in advance.

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

Joined: 19 June 2002
Location: United States
Status: Offline
Points: 376
Post Options Post Options   Thanks (0) Thanks(0)   Quote Flamewave Quote  Post ReplyReply Direct Link To This Post Posted: 17 October 2003 at 7:49am

You could give them all a unique ID numbering 1 through 20, with no missing numbers, and then have ASP generate a random number between 1 and 20, then use a select statement like this: "select * from eCatalog where CatID = " & intRandomNumber

Otherwise, you can put the recordset into an array, and then do the same thing more or less only this time you would be accessing the array: myArray(intRandomNumber)

For more information on copying a recordset to an array, click the link below.
http://www.w3schools.com/ado/met_rs_getrows.asp

- Flamewave

They say the grass is greener on the other side, but if you really think about it, the grass is greener on both sides.
Back to Top
MorningZ View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 May 2002
Location: United States
Status: Offline
Points: 1793
Post Options Post Options   Thanks (0) Thanks(0)   Quote MorningZ Quote  Post ReplyReply Direct Link To This Post Posted: 17 October 2003 at 8:40am

if its a MsSQL you can use the following SPROC
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=173

Alternative database (like Access)
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=65

Contribute to the working anarchy we fondly call the Internet
Back to Top
cosmic cat View Drop Down
Groupie
Groupie


Joined: 14 May 2002
Location: United Kingdom
Status: Offline
Points: 49
Post Options Post Options   Thanks (0) Thanks(0)   Quote cosmic cat Quote  Post ReplyReply Direct Link To This Post Posted: 17 October 2003 at 7:08pm

A really elegant way to do this if you've got SQL Server 7 and above is:-

SELECT TOP nn * FROM my_table ORDER BY NewId()

(where nn is the number of random items you want and my_table is the table you are retrieving the data from)

This adds a GUID to each row and will return random records without the need for complex stored procs or asp routines.

Back to Top
cosmic cat View Drop Down
Groupie
Groupie


Joined: 14 May 2002
Location: United Kingdom
Status: Offline
Points: 49
Post Options Post Options   Thanks (0) Thanks(0)   Quote cosmic cat Quote  Post ReplyReply Direct Link To This Post Posted: 17 October 2003 at 7:15pm

I've also found an Access 97/2000 version:-

SELECT TOP 1 id, strText FROM tblMyTable ORDER BY Rnd(id)

This needs an autonumber field, id in the table to work as a seed for the Rnd() function to make the records completely random.

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

Joined: 06 May 2002
Location: United States
Status: Offline
Points: 1793
Post Options Post Options   Thanks (0) Thanks(0)   Quote MorningZ Quote  Post ReplyReply Direct Link To This Post Posted: 17 October 2003 at 7:41pm
Originally posted by cosmic cat cosmic cat wrote:

A really elegant way to do this if you've got SQL Server 7 and above is:-

SELECT TOP nn * FROM my_table ORDER BY NewId()

(where nn is the number of random items you want and my_table is the table you are retrieving the data from)

This adds a GUID to each row and will return random records without the need for complex stored procs or asp routines.

whoa.. excellent little tidbit there!!  nice

Contribute to the working anarchy we fondly call the Internet
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: 18 October 2003 at 10:28am
Originally posted by cosmic cat cosmic cat wrote:

I've also found an Access 97/2000 version:-

SELECT TOP 1 id, strText FROM tblMyTable ORDER BY Rnd(id)

This needs an autonumber field, id in the table to work as a seed for the Rnd() function to make the records completely random.

 

I tried this but didn't work:

 

I used this..

<%
Dim Rand, RandS, RandHere
Rand = "SELECT TOP 1 id FROM TableName ORDER BY Rnd(id)"
set RandS = server.createobject("adodb.recordset")
RandS.open Rand, Conn, 3, 1

response.write RandS(0)
%>


the database tested has 7 items... always id # 7 displayed,

everytime the only displayed record is the last one, I refreshed 20 times.. but the last record is the only displayed.

i modified the code to:  ORDER BY Rnd(id) DESC.. always id 6 displayed...

 

 

I'm trying the new posts now

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: 18 October 2003 at 10:48am

 no success yet.. they are too complicated...

i am using access...



Edited by zMaestro
Back to Top
 Post Reply Post Reply Page  123>

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.