Jump to content

How to choose random person on a database ( For a Game Show )


patmagpantay

Recommended Posts

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?

 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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>" ;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.