PHP loops and incrementing a variable

Hello, I always get good help here and I'm lost.. did a bit of searching ans still couldn't get my code working.

I need to increment 2 variables: $i and $n

I have an array with: "Games," "username," "nextGame" and "winner"

This is my code:

<?php

foreach($games_2 as $element) {
$i=0;
$i2=$i++;
$n=8;
$n2=++$n;  // also tried $n2=$n++;

  if ($element['game'] >= $i2) {
      echo ' <br>user: ' . $element[username];
      echo ' <br>nextGame: ' . $element[nextGame];
      echo ' <br>winner: ' . $element[winner];
      echo ' <br>n: ' . $n2;
    }  
}
?>

I get the following output:

user: jay
nextGame: 9
winner: d
n: 9
user: jay
nextGame: 9
winner: a
n: 9
user: jay
nextGame: 10
winner: e
n: 9
user: jay
nextGame: 10
winner: g
n: 9
user: jay
nextGame: 11
winner: i
n: 9

$i2 is clearly incrementing and if I change to $i=3 it starts the echos with game 4 -- just like I want.

the problem is I need $n2 to increment after each game lasted (that will become the number for the winner's next game. So that winner "d" next game is game 9 and "winner a" next game is game 10 and "winner

Draw a flowchart to organize your idea. It will show you what you need.

1 Like

There is a BIG difference between "$n2 = $n++" and "$n2=++$n". Are you sure you understand that difference?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.