need to loop through multiple statements with specific grouping
<p>I have the following php code:</p>

<pre><code>$k=1;
for($i=0; $i &lt; 5; $i++)
{
    $stmt = $db-&gt;query('SELECT * FROM services_main');
    while($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) {

        if ($row['ID'] == $i) {

            for($j=1; $j &lt;= 3; $j++)
            {
                echo $row['name'].' - k='.$k++.' - j='.$j.' | ';
                echo $row['color'].' - k='.$k++.' - j='.$j.'&lt;br /&gt;';

            }

        }
    }
}
</code></pre>

<p>This code generates this:</p>

<pre><code>John - k=1 - j=1 | Blue - k=2 - j=1
John - k=3 - j=2 | Blue - k=4 -  j=2
John - k=5 - j=3 | Blue - k=6 -  j=3
Paul - k=7 - j=1 | Green - k=8 -  j=1
Paul - k=9 - j=2 | Green - k=10 -  j=2
Paul - k=11 - j=3 | Green - k=12 -  j=3
George - k=13 - j=1 | Red - k=14 -  j=1
George - k=15 - j=2 | Red - k=16 -  j=2
George - k=17 - j=3 | Red - k=18 -  j=3
</code></pre>

<p>I would like it to generate this:</p>

<pre><code>John - k=1 - j=1 | Blue - k=1 - j=1
John - k=2 - j=2 | Blue - k=2 -  j=2
John - k=3 - j=3 | Blue - k=3 -  j=3
Paul - k=4 - j=1 | Green - k=4 -  j=1
Paul - k=5 - j=2 | Green - k=5 -  j=2
Paul - k=6 - j=3 | Green - k=6 -  j=3
George - k=7 - j=1 | Red - k=8 -  j=1
George - k=8 - j=2 | Red - k=8 -  j=2
George - k=9 - j=3 | Red - k=9 -  j=3
</code></pre>
