patmagpantay Posted December 17, 2007 Share Posted December 17, 2007 Hello! I have set up a script form called " form tools " without a problem, This script gathers all information in the registration form and stores it into a database (php and mysql), Now I would like to randomly choose 5 people from the data I collected from the database randomly, Since it’s a game, I would like it to be as random as possible, how do I do this? Quote Link to comment Share on other sites More sharing options...
dbo Posted December 17, 2007 Share Posted December 17, 2007 1) You would run a query to pull back all of the users (only their ids might be enough). 2) You would then determine the total number of results returned. 3) You would use the rand function to select an index between 0 and number of rows returned. Also making sure that it does not existing in your array of already selected users. 4) You would store the user at that index in an array. 5) Repeat the process as needed, but not exceeding the total length of your user results. (You can't randomly select 5 unique people if there are only 4 in existence, etc) Quote Link to comment Share on other sites More sharing options...
timmah1 Posted December 17, 2007 Share Posted December 17, 2007 This is what I use for my random banner code All you'll have to do is change the $num_displayed = 1 ; to however many people you want <? // Connect to the database mysql_connect ('xxx', 'xxx', 'xxx') ; mysql_select_db ('xxxx'); // Edit this number to however many links you want displaying $num_displayed = 1 ; // Select random rows from the database $result = mysql_query ("SELECT * FROM table ORDER BY RAND() LIMIT $num_displayed"); // For all the rows that you selected while ($row = mysql_fetch_array($result)) { // Display them to the screen... echo "<a href=\"" . $row["link"] . "\"> <img src=\"banners/" . $row["image"] . "\" border=0 title=\"" . $row["title"] . "\"> </a>" ; } ?> Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 17, 2007 Share Posted December 17, 2007 SELECT * FROM table ORDER BY RAND() LIMIT 1; or some of the methods here. Quote Link to comment Share on other sites More sharing options...
dbo Posted December 17, 2007 Share Posted December 17, 2007 I like Daniels! But yeah, there are a lot of different solutions to this problem, shouldn't be too bad to figure out. Just give it a shot and post to us if you have questions. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.