Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping to display all data in a multidimensional array (getRows() or other array)

ASP 102

Looping to display all data in a multidimensional array (getRows() or other array)

by  Abraxus  Posted    (Edited  )
Multidimensional arrays can be tricky to work with. One of the most common ways of producing a multidimensional array is by creating a recordset and then dumping the data into an array using the ASP getRows() command. This is a quick FAQ answer to explain how to work with multidimensional arrays.
Code:
[color green]' Assuming we have created a connection, executed some SQL,
' and opened a recordset named rs
' Now we can dump all of the data in rs into an array called rsArray[/color]
rsArray = rs.getRows()
[color green]
' An advantage of getRows() - rs object is no longer needed in memory[/color]
rs.Close
set rs = nothing
[color green]
' If the array is a large grid of data, you identify
' the columns as (rsArray, 1) and the rows as (rsArray, 2)
' A single item in the array is located at rsArray(column, row)[/color]
colStart = LBound(rsArray, 1)
colEnd   = UBound(rsArray, 1)
rowStart = LBound(rsArray, 2)
rowEnd   = UBound(rsArray, 2)
[color green]
' Loop through all rows of the array and display the data[/color]
Response.Write("<table>" & chr(13))
For row=rowStart to rowEnd
   Response.Write("<tr>" & chr(13))
   [color green]' For each row we loop through every column[/color]
   For col=colStart to colEnd
      Response.Write(chr(9) & "<td>" & rsArray(col,row) & "</td>" & chr(13))
   Next
   Response.Write("</tr>" & chr(13))
Next
Response.Write("</table>" & chr(13))
The chr commands in the output lines insert tabs and carriage returns to format the source for easy viewing.

If you have any questions about multidimensional arrays or suggestions for expanding this FAQ please drop me a note!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top