need to increment a group of results only using multiple loop statements
<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++.'&lt;br /&gt;';
                }

            }
        }
    }
</code></pre>

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

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

<p>What i am trying to do it have it generate this:</p>

<pre><code>    John - k=1
    John - k=1
    John - k=1
    Paul - k=2
    Paul - k=2
    Paul - k=2
    George - k=3
    George - k=3
    George - k=3
</code></pre>

<p>I can't figure out how to make k increment for each grouping as opposed to incrementing on every single line.</p>

<p>Thanks in advance for your help!</p>
