Dynamically create a 3x2 html table
WizzKidd | Posted 5:06pm 23. April 2008 Server Time |
I cant get my head around how to write this loop.
At the moment I've decided I would like to output my results from the query dynamically into a 3x2 table... 3 columns, 2 rows...
eg. my query returns the following if i use a basic WHILE loop to loop through the recordset...
1.Apples
2.Pears
3.Oranges
4.Plums
5.Peaches
6.Mangos
I would like to dynamically build a HTML table in ASP so that the output would look something like:
<table>
<tr>
<td>1.Apples</td>
<td>2.Pears</td>
<td>3.Oranges</td>
</tr>
<tr>
<td>4.Plums</td>
<td>5.Peaches</td>
<td>6.Mangos</td>
</tr>
</table>
I'd appriciate some help here so I can get my head around the loops (im soooo rusty)... Then eventually, I may even be able to make a 3x3 or 4x3 table myself by adjusting a few numbers here or there.
Thanks in advanced.
- Neil-One (aka WizzKidd)
- http://www.promotioncity.co.uk |
katy8439 | Posted 4:47am 24. April 2008 Server Time |
Try the following. Change MaxCellsPerRow to vary how the table looks
<%
'** Array used for simplicity, could easily be a recordset
DIM MyArray(6)
MyArray(0)="Apples"
MyArray(1)="Pears"
MyArray(2)="Oranges"
MyArray(3)="Plums"
MyArray(4)="Peaches"
MyArray(5)="Mangos"
MyArray(6)="Avacado"
'** How many records are we dealing with?
TotalRecords = UBound(MyArray)
'** How many cells are we putting across the table?
MaxCellsPerRow = 3
'** Don't want more Cells per row than our total
IF MaxCellsPerRow > TotalRecords THEN
MaxCellsPerRow = TotalRecords
END IF
'** Counter to hold our current position
CellCounter = 0
'** Starting table and row tags - border added for clarity
Response.Write "<table border=""1""><tr>"&vbcrlf
'** Start looping through our records
FOR RecordLoop = 0 TO TotalRecords
IF CellCounter = MaxCellsPerRow THEN
'** We've reached the limit of the cells for this row
'** close the row and start a new one
Response.Write "</tr><tr>"&vbcrlf
'** Reset the counter for a new row
CellCounter = 0
END IF
'** Write out the record
Response.Write "<td>"&MyArray(RecordLoop)&"</td>"&vbcrlf
'** Increment our place in the row
CellCounter = CellCounter+1
NEXT '** Move to Next Record
'** In this example we have an odd number of cells which
'** means that the table will be missing some <td>'s
'** How many cells do we have left over (minus 1 as we're starting at zero)?
RemainingCells=(MaxCellsPerRow - CellCounter)-1
FOR FinishTableLoop = 0 TO RemainingCells
Response.Write "<td> </td>"&vbcrlf
NEXT
'** Close off the table
Response.Write "</tr></table>"&vbcrlf
%>
WizzKidd | Posted 5:41am 24. April 2008 Server Time |
Wow! thats an interesting method. But it is very clearly commented which means that I've understood your method entirely, although I found a method that uses the MOD operator.
Thanks for your help again.
- Neil-One (aka WizzKidd)
- http://www.promotioncity.co.uk
Reply to Post Dynamically create a 3x2 html table
|
|
|