Query for random list

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello

I have FAQ’s section in my site with information coming from a database and I would like to create a random list of five of those FAQ’s each time the page is loaded. My question is how the query should look like.

Thanks in advance
 
Hi Vladymir,

There is no direct SQL predicate to allow this. To accomplish you could use ASP script with the Rnd function to produce a random
number. -- use randomize to set the seed so that the same numbers are not produced each time.

One problem then you'll have is that you'll need to get the number of records within the db so that you can produce a random number
between the lower bound and upper bound of records. The following formula will restrict the return value to that range.
Int((upperbound - lowerbound + 1) * Rnd + lowerbound)Even though it is possible to retrieve records by record number I would
strongly suggest that you use getRows then pick the records from the created array by using the above formula and a for loop.If this
is more that what your skill level will allow you may wish to just display the Top 10 accessed FAQ by updating an addition field
with the db each time it is accessed. You can then use the predicate TOP in the SQL statement to return the top 'n' FAQ's.HTH,--
Mike -- FrontPage MVP '97-'02http://www.websunlimited.comStop Spam Email Mining from your web pages with
SpamStopperhttp://www.websunlimited.com/order/product/SpamStopper/spam_stopper_help_dir.htmFrontPage Add-ins Since '97 2003 / 2002 /
2000 Compatible

Vladymir said:
Hello

I have FAQ's section in my site with information coming from a database and I would like to create a random list of five of those
FAQ's each time the page is loaded. My question is how the query should look like.
 
Works -- provided your database is MS SQL Server.

--
Mike -- FrontPage MVP '97 - '02
http://www.websunlimited.com
Need to use ASP in a FrontPage 2003 include component? Well you can with IncludeASP!
http://www.websunlimited.com/order/Product/IncludeASP/IncludeASP.htm
http://www.websunlimited.com/order/product/includeASP/includeASP.htm


I've used this method a few times, there are other ways to
do this.

http://www.4guysfromrolla.com/webtech/081100-1.shtml

Bill Schroyer
(e-mail address removed)
http://www.frontpagewiz.com
-----Original Message-----
Hello

I have FAQâ?Ts section in my site with information coming
from a database and I would like to create a random list
of five of those FAQâ?Ts each time the page is loaded. My
question is how the query should look like.
 
Howdy.

I would do a query to retrieve all the primary keys,
taking care to specify cursor properties that made the
recordset's RecordCount property available.

Then, I would divide the RecordCount property by 5 to get
a sample size (i.e. of there are 82 tips, sample size is
82/5 = 16

Then, I would multiply by a random number and add 1.

1st tip = 1 + (16 * Rnd())
2nd tip = 1 + 16 + (Rnd() * 16)
3rd tip = 1 + 32 + (Rnd() * 16)
4th tip = 1 + 48 + (Rnd() * 16)
5th tip = 1 + 64 + (Rnd() * (82 - 64))

Of course, after calculating each tip number, I would move
to that position in the recordset, retrieve the primary
key value, and then do a lookup and display for that key
value.

You could also pick a random key from the whole range
(i.e. 82) each time, but then you'd have to check for
duplicates.

Jim Buyens
Microsoft FrontPage MVP
http://www.interlacken.com
Author of:
*----------------------------------------------------
|\---------------------------------------------------
|| Microsoft Office FrontPage 2003 Inside Out
||---------------------------------------------------
|| Web Database Development Step by Step .NET Edition
|| Microsoft FrontPage Version 2002 Inside Out
|| Faster Smarter Beginning Programming
|| (All from Microsoft Press)
|/---------------------------------------------------
*----------------------------------------------------

-----Original Message-----
Hello

I have FAQâ?Ts section in my site with information coming
from a database and I would like to create a random list
of five of those FAQâ?Ts each time the page is loaded. My
question is how the query should look like.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top