Since you are going to make it integrated with threads I'd recommend using the threads way of doing templates.
Thus, you should first put the categories in an array, possibly with subcategories as an array in the array of main categories.
Then, in the template, you have a loop that goes through the array and outputs table cells for each main category. For each iteration of the loop you increment a variable by one, and if that number can be divided by the number of columns you want, you just print the </tr>.
Something like this:
<br />$CatColumns = 3;<br />$Numberofcats = 15;<br /># This calculates the number of cells we need.<br />if ( $Numberofcats % $CatColumns != 0 ) {<br /> $cells = $Numberofcats + ( $CatColumns - ( $Numberofcats % $CatColumns ) );<br />} else {<br /> $cells = $Numberofcats;<br />}<br /><br />for ($i = 1; $i <= $cells; $i++) {<br /> # Check if this is the first cell of the row.<br /> if ( ($i - 1) % $CatColumns == 0 ) {<br /> print "<tr>\n";<br /> }<br /> # Make sure we have something to print.<br /> if ( $i <= $Numberofcats ) {<br /> print "<td> $i. {$catarray[$i]['name']} </td>\n";<br /> } else {<br /> print "<td> <!-- empty cell --> </td>\n";<br /> }<br /> # Check if this is the last cell of the row.<br /> if ( $i % $CatColumns == 0 ) {<br /> print "</tr>\n";<br /> }<br />}<br />