Velkyr Velkyr - 2 months ago 13
PHP Question

Multiple while loops?

Trying to figure out a decent way to get a multiple while loops to work properly.

What I have been using is:

$x = "0";
$y = "0";

while($x <= "7"){

while($y <= "7"){

echo $x . "-" . $y ."<br />";

$y++;
}
$x++;
}


Trying to get that to display:

1-1
1-2
1-3
...
1-7
2-1
2-2

etc

Anyone able to provide a quick snippet?

Answer

You need to reset y after it exits the while loop:

$x = "0";
$y = "0";

while($x <= "7"){

    while($y <= "7"){

        echo $x . "-" . $y ."<br />"; 

        $y++;
    }

    $y = "1";
    $x++;
}
Comments