Developers often have sets of things they want to present to a user, and the typical way is to process each element and then put some kind of vertical space (e.g. <hr> or <p>).
Many times, these developers want an easy way to display more information without all the vertical space. Image thumbnails, for example, that are 20 pixels wide, don't need their own 'row' in most HTML interfaces. So what we want to do is say "let's display n of these per row, and when we reach n, start a new row." Here is a code sample that will handle any number of elements, and any number of columns. Download the code and try it out; just change the first two values and play with it. This code should be easy to adapt to your recordsets, array processing, etc.
<% ' number of elements, e.g. recordcount numElements = 11 ' # of columns in the table: numCols = 5 ' populate some dummy data dim tmpArray() redim tmpArray(numElements-1) for k = 0 to numElements-1 tmpArray(k) = "Item " & cstr(k+1) next ' all right, let's start the table response.write "<table border=1>" ' enter the loop for i = 0 to numElements-1 ' control variable, to determine if we need ' to pad the last column(s) with space needFooter = true if i mod numCols = 0 then ' beginning of row response.write "<tr>" end if ' display each cell response.write "<td>" & tmpArray(i) & "</td>" if i mod numCols = numCols - 1 then ' end of row, so don't need to pad response.write "</tr>" needFooter = false end if next if needFooter then ' okay, we need to put in the balance columns response.write "<td colspan=" &_ numCols - (i mod numCols) &_ "> </td></tr>" end if response.write "</table>" %> |
If you were using recordset data, you would only adjust a few items. The following code assumes an open recordset called rs, that is not EOF:
<% numCols = 5 i = 0 do while not rs.eof needFooter = true if i mod numCols = 0 then response.write "<tr>" end if response.write "<td>" & rs(0) & "</td>" if i mod numCols = numCols - 1 then response.write "</tr>" needFooter = false end if i = i + 1 rs.movenext loop if needFooter then response.write "<td colspan=" &_ numCols - (i mod numCols) &_ "> </td></tr>" end if response.write "</table>" %> |