What I think you're saying you want to do is that every time the 3rdly nested loop is run, the $incrament variable needs to be one higher? Is that right? I noticed you never actually set the $incrament variable. If I understand you correctly, all you should have to do is define $incrament at the start of the loops
$incrament = 0; // or whatever start value you want
for($i=0;$i<sizeof($pages);$i++)
{ ...
Then it's just a matter of deciding when you want to increment it. If you want $incrament to be 1 higher every time you hit that loop, then put $incrament++; right after the closing } of the 3rd loop so it gets incremented just before reading the next line out of $results. Otherwise, you can put it after the closing } of the while loop so it gets incremented before the reading of the next page from the DB. Hope that helps.
Adam