THat is something you should figure out yourself. Not to be harsh, but it is not coding dificulty, but pure logic that is required here. I know you can figure it out, as the code you have shown pretty much has all of it. Just note you need to set $i=0; before you enter the while() loop, and add one to the $i on each line like so: $i++;
Then all you need to do is count lines, loops and of course ensure you finalize the table.
Naturally, I would not use a table, but free-floating <div>'s which you stack using CSS.