Skip to content
Advertisement

PHP for loop only works every second row

in my current project I want to split 10 users from a queue into 2 teams with 5 users each.

When 10 users are in the queue the function create_new_lobby() is executed. In this function the match is created and all users should be assigned to the teams in an intermediate table. With a for loop all users should be assigned to the match. Currently the for loop only takes every second user (0, 2, 4, 6, 8). The values 1, 3, 5, 7 & 9 are apparently skipped by the for loop.

Where is the error here?

JavaScript

Current result: Current Database Output

Expected result: Expected result

Advertisement

Answer

The for loop works as it should and you can check that by placing an var_dump($payer) inside the for loop;

The increment in the for loop is given by the third statement, the $player++, which means $player = $player + 1;

If you would want to have a different looping step, you can add a different expression for the third statement in the for loop.

e.g.

JavaScript

The code snippet will output 0 1 2 3 4 5 6 7 8 9

JavaScript

The code snippet will output 0 2 4 6 8

I hope this helped you better understand the for loop.

The actual problem you are facing is created by the logic that you have made with SQL and not by the for loop

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement