0

Say this table :

User

id | username          | highest_score 
1  | detectivepikachu  | 1000          
2  | fullmetaljacket   | 4000          
3  | sonicthegreat     | 8000
4  | inspectorgadget   | 2000
5  | themartian        | 8000

I need to loop through all users highest scores and pick the ones with a score higher to 5000. I'm using a while loop.

while($user = $user_result-> fetch_assoc()){

if($user['highest_score'] >= 5000){
echo $user['username'];
echo '<br>';
}

}

This while statement will work and will display

sonicthegreat 
themartian 

But what I want is only displaying one of them randomly. How can I limit only one row to be displayed?

  • Well in your response it is doing so in the immediate query which is not what I'm trying to do. I'm trying to do it in the while statement if that even makes any sense at all to do it like that. –  Jan 11 '19 at 15:48
  • You could load those users into an array inside your `while` and then pick a [random array element](http://php.net/array_rand) to display (if you wanted to do this on the PHP side). I would do this in SQL though like `SELECT username FROM table WHERE highest_score > 5000 ORDER BY RAND() LIMIT 1;` and just display whatever username is returned. – JNevill Jan 11 '19 at 15:49

4 Answers4

1

This will echo only the first user with a score equal to or higher than 5000:

while($user = $user_result->fetch_assoc())
{
    if($user['highest_score'] >= 5000)
    {
        echo $user['username'];
        break;
    }
}
1

You can simply break out of the while loop after you print.

while($user = $user_result->fetch_assoc()){

    if($user['highest_score'] >= 5000){
        echo $user['username'];
        echo '<br>';
        break;
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Here is how it's done. You need first to put the winners in a new array and then pick a random number to choose a random player.

<?php
$user[] = ["id" => 1, "username" => "detectivepikachu", "highest_score" => 1000];
$user[] = ["id" => 2, "username" => "fullmetaljacket", "highest_score" => 4000];
$user[] = ["id" => 3, "username" => "sonicthegreat", "highest_score" => 8000];
$user[] = ["id" => 4, "username" => "inspectorgadget", "highest_score" => 2000];
$user[] = ["id" => 5, "username" => "themartian", "highest_score" => 8000];


$nb_user = count($user);
$array_score = array();
while($nb_user > 0){
    $nb_user--;
    if($user[$nb_user]['highest_score'] >= 5000){
        $array_score[] = [$user[$nb_user]['username']];
    }
}

$rand = rand(0, count($array_score) -1);
echo $array_score[$rand][0];
executable
  • 3,365
  • 6
  • 24
  • 52
0

The other answers do not choose one of the results randomly, this is how you can make it choose one randomly.

Loop through all your users, and add any users with a score above 5000 to an array.

$high_scores = array();

//foreach instead of while is better for arrays
//or you can use while($user = $user_result->fetch_assoc()){
foreach($users as $user) {
    if($user['highest_score'] >= 5000) {
        $high_scores[] = $user;
    }
}

//choose a random number between 0 and the amount of highscores (-1 to account for arrays starting at 0)
echo $high_scores[rand(0, count($high_scores)-1)]['username'];

Once they are in an array, as you can see in the code, you can choose one of those users randomly by using the rand() function. The min value for rand() should be 0, since arrays start at index 0, and the max should be the amount of entries in the array -1 (Because in an array with 3 entries, the last one would be index 2)

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71