Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > rotating db info...

rotating db info...
Thread Tools
Registered User
Join Date: Sep 2002
Location: New York City
Status: Offline
Reply With Quote
May 2, 2004, 05:06 PM
 
Hi,

I have a database table with several hundred rows. Each row has a status of either "on" or "off".

What I'd like to do is create a PHP page that will randomly select 5 rows from the db table, where the rows are "on". For example, you visit the page and it will display rows 2, 45, 122, 7, and 19. You hit your browser's Refresh button and it will display rows 98, 475, 13, 111, and 55.

One of the challenges is that the "off" rows will be randomly distributed among the "on" rows. So the "on" are not cleanly ordinal.

Any ideas of how to do this would be greatly appreciated.

Thanks in advance.

-Ben
     
Mac Elite
Join Date: Mar 2001
Location: England
Status: Offline
Reply With Quote
May 2, 2004, 06:09 PM
 
SELECT * FROM table_name WHERE status='on' ORDER BY RAND() LIMIT 5

No PHP needed!
What the nerd community most often fail to realize is that all features aren't equal. A well implemented and well integrated feature in a convenient interface is worth way more than the same feature implemented crappy, or accessed through a annoying interface.
     
Registered User
Join Date: Sep 2002
Location: New York City
Status: Offline
Reply With Quote
May 3, 2004, 01:21 AM
 
Originally posted by Amorya:
SELECT * FROM table_name WHERE status='on' ORDER BY RAND() LIMIT 5

No PHP needed!
THAT'S AWESOME!

I didn't know that "Rand()" existed for mysql.

Thanks a ton.
     
Junior Member
Join Date: May 2003
Status: Offline
Reply With Quote
May 3, 2004, 11:46 AM
 
My website exists because of Rand(), it's excellent
www.jmarus.com - kicks by JM
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:28 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2