You know, there really was no reason to have the Y and W columns in that table.
MySQL could easily derive both of those from the DT field.
But oh well, since you have them, let's use them.
Anyway, the point you are completely missing is *WHAT* you want to show for each week.
That is, do you want to show the detailed payroll (every paycheck for every person) for each week? Or so you want to show the total payroll for the week? Or what?
No, you CERTAINLY should *NOT* do a FOR loop getting one week at a time. Yuck.
But you need to tell us what data you want to display for each week.
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.