generally I use modulo or % to do this.
basically, set your $loop = 0;
if ($loop % 7) { print "</tr><td>" }
$loop modulo 7 = 0 when there is no remainder, i.e., the column is divisible by 7 (or if the loop is 0) You need to do some special case handling for 0, and some end of loop processing if there aren't a multiple of 7 columns in your result set.
The reason for your extra column is probably that you started loop at 1, and are dividing by 7, which gives you a first blank column -- then things should realign themselves unless there is something else I'm not seeing. From what you posted, you should get 6 columns in the first row, 7 in each successive row.
Another way is to use an indexed array and use two for loops...
Code:
print "<table>";
for ($loop=0;loop<$numcols;loop+=7) {
print "<tr>";
for ($loop2=$loop;$loop2<$loop+7;$loop2++) {
print "<td>asdfa</td>";
}
print "</tr>";
}
print "</table>";
Many different ways to handle tabular data.... and they all boil down to a simple control-break structure.