| Author |
Topic Search Topic Options
|
zMaestro
Senior Member
Joined: 11 May 2003
Location: Egypt
Status: Offline
Points: 1183
|
Post Options
Thanks(0)
Quote Reply
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.
|
 |
Flamewave
Senior Member
Joined: 19 June 2002
Location: United States
Status: Offline
Points: 376
|
Post Options
Thanks(0)
Quote Reply
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.
|
 |
MorningZ
Senior Member
Joined: 06 May 2002
Location: United States
Status: Offline
Points: 1793
|
Post Options
Thanks(0)
Quote Reply
Posted: 17 October 2003 at 8:40am |
|
|
|
Contribute to the working anarchy we fondly call the Internet
|
 |
cosmic cat
Groupie
Joined: 14 May 2002
Location: United Kingdom
Status: Offline
Points: 49
|
Post Options
Thanks(0)
Quote Reply
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.
|
 |
cosmic cat
Groupie
Joined: 14 May 2002
Location: United Kingdom
Status: Offline
Points: 49
|
Post Options
Thanks(0)
Quote Reply
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.
|
 |
MorningZ
Senior Member
Joined: 06 May 2002
Location: United States
Status: Offline
Points: 1793
|
Post Options
Thanks(0)
Quote Reply
Posted: 17 October 2003 at 7:41pm |
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
|
 |
zMaestro
Senior Member
Joined: 11 May 2003
Location: Egypt
Status: Offline
Points: 1183
|
Post Options
Thanks(0)
Quote Reply
Posted: 18 October 2003 at 10:28am |
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
|
 |
zMaestro
Senior Member
Joined: 11 May 2003
Location: Egypt
Status: Offline
Points: 1183
|
Post Options
Thanks(0)
Quote Reply
Posted: 18 October 2003 at 10:48am |
no success yet.. they are too complicated...
i am using access...
Edited by zMaestro
|
 |