The trick to this is using the modulus operator. As you loop through the records, you want to use a counter. The counter will be mod against 3. For instance....
CODE
$counter = 1;
// We loop through a recordset we pulled from the database.
while ($row = mysql_fetch_array($result)) {
if (($counter % 3) == 1) {
echo "<tr><td>{$row['field1']}</td>";
}
else if (($counter % 3) == 2) {
echo "<td>{$row['field2']}</td>";
}
else { echo "<td>{$row['field3']}</td></tr>"; }
$counter++;
}
// Test to see if we need a closing row tag (count was not divisible by 3)
if ((($counter - 1) % 3) > 0) { echo "</tr>"; }
So what this does is this, if the counter was 5 it would first trigger the first if creating
<tr><td>{$row['field1']}</td> and then it would go 2 and print
<td>{$row['field2']}</td> and then it would go 3 and print
<td>{$row['field3']}</td></tr> closing off the row. It would then go 4 and print
<tr><td>{$row['field1']}</td> and lastly hit 5 and print
<td>{$row['field3']}</td></tr> and exit the loop, but it would check 5 % 3 = 2 so it would tack on the closing row tag. Resulting in an output of...
CODE
<tr><td>data1</td>
<td>data2</td>
<td>data3</td></tr>
<tr><td>data4</td>
<td>data5</td>
</tr>
which as you can see will give you one row of 3 items and a second row of two items.
Hopefully you get the idea by manipulating the counter with a mod operator (%) you will be able to dictate the pattern. It will only create a new row when the multiple has been filled. In this instance, after it hits three cells, it will start a new row.