SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool Help me chose a Conest Winner

    Ok guys,

    Need some help again as I need to brush up on my rusty PHP programming for a client. What he wants is a contest system, 90% of it is complete, I just badly need help with this last part, the randomly choosing of the winning user(s). They eligible users are in a table called contest and there is a column that defines what round we are in. I need to randomly select 3 people (first, second, third) to win. They cant win twice! (or three times for that matter).

    What I was thinking was to have an array fetch the usernames WHERE round=$round_number and from there, have rand() chose a winner?

    I have never really understood arrays, so thatís why I need help.

    This is what I am thinking of as a script. Please help me out (the below code probably doesnít work).



    PHP Code:
    $result mysql_query("SELECT username FROM contest WHERE round='$round_number'" );
    while (
    $row mysql_fetch_array($result)) {
    // some how assign a number?

    round(
    // Some how chose a winner?
    ); 


    Thank you all, Any help would be great.

  2. #2
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how about this:

    PHP Code:
    mysql_query('SELECT RAND(), RAND(), RAND()');
    $result mysql_query("SELECT username FROM contest WHERE round=$round_number ORDER BY RAND() LIMIT 3");
    while(
    $row mysql_fetch_array($result)){
        
    //print your rows ...


  3. #3
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should generate 3 random winners from the result set, no matter how large the result set is, as long as you are only selecting the username:
    PHP Code:
    for($i 0$i 3$i++)
    {
      
    $winningNumber rand(0, (mysql_num_rows($result) - 1));
      
    $winner[$i] = mysql_result($result$winningNumber);

    Then, if you need to, you can use the $winner[] array to set a field in the database making them ineligible to win again.
    John

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by platinum
    how about this:
    PHP Code:
    mysql_query('SELECT RAND(), RAND(), RAND()');
    $result mysql_query("SELECT username FROM contest WHERE round=$round_number ORDER BY RAND() LIMIT 3" );
    while(
    $row mysql_fetch_array($result)){
    //print your rows ...

    platinum,

    How would I go about printing it? $row[0]; ?

  5. #5
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, sorry I could have done it, but I wasn't thinking

    print $row['username'] . '<br />';

    should do it


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •