Help with logic in SELECT

Asked

Viewed 52 times

1

I have a "matchmaking" script (like a game). He checks every 3 seconds a table, to see if there are 10 players who are not playing. Only that I also need to check if among these 10 players, one of them is me.

The select is like this

$busca_db_qtd = mysqli_query($conexao, "SELECT ativo, gamemode, playing, id_user FROM users_buscando WHERE ativo = '1' and gamemode = '$gamemode' and playing = '0' and reservados = '0' LIMIT 10");

I need to check these 10 players, one of them has my id. How can I do this, without having to do a while in php and use a lot of processing (I think it uses right) ?

  • 2

    Iterating 10 records does not seem to be something that will use a lot of processing. The player sending the query himself should not appear in this result? If yes, you can restrict the query by doing id_user!=$id, for example.

  • That’s what I did, Joao; :)

1 answer

0

Tries to put the player Ids you find when running the macthmaking script in an array $array players.

array_push($array, $jogador->id);

Then just check the array for duplicates.

function has_dupes($array) {
$dupe_array = array();
foreach ($array as $val) {
    if (++$dupe_array[$val] > 1) {
        return true;
    }
}
return false;
}

Function "Function has_dupes" found in Link to source by @Mike Sherov

Browser other questions tagged

You are not signed in. Login or sign up in order to post.