Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Display of Image 2

Status
Not open for further replies.

Faheem786

Programmer
Sep 5, 2001
30
0
0
HK
Hi there,

I have a database driven website for showing the products. I have a field called featured products which could be more than one.

On the Homepage I would like display one product and the related link out of the featured products. But this should be changed randomly.

What I mean is everytime when the user goes to the homepage he/she should be able to see a different product and the related link based on some unique values.

If possible I also want to diplay them as a slide show in a random order with the appropriate link to it.

Can U experts pls help me how can I do this??

Thanks in advance

Faheem
 
A simple way of doing it is:
a) fetching a recordset with all the products
b) checking the recordcount of the recordset
c) generate a random number between 1 and the recordcount
d) go to the record at the position of the generated number (use AbsolutePosition Property if an ADO Recordset)
e) print the product data from the record

If you want several products, repeat step c to e. This can give duplicates, so if you want to avoid them use another approach.
 
This method is *OK* but not ideal. Every time a user loads the page you are returning the entire list of products. This is a) going to slow down query performance and b) increase network traffic - not good if you are dealing with a large web site selling thousands of products.

Ideally you would only return one random record each time. Say you have a Product table with a bit (1/0) field called IsFeature. You could return a random product using:

Code:
SELECT TOP 1 *
FROM product
WHERE isfeature = 1
ORDER BY NEWID()

The NEWID() function returns a uniqueidentifier value for each row and therefore creates the random order for you. Then you use TOP 1 to return just one record.

NB: This query is valid for SQL Server (and I think Access too). not sure about other RDBMS. --James
 
Nice solution James! I didn't know that it is valid to order by something not in the FROM clause.
And I agree that my solution isn't ideal.
 
The subject of random SQL queries came up recently and was investigated in some depth by Tarwn. Check out this thread:

thread333-452982 --James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top