This tutorial teaches you how to create a multidimensional resizable array in ASP/VBscript.
In the following code snippet you will find out how to create a multidimensional resizable array.The first step is to declare the array using the number of fields per level or index that you want. For this example we will use 4 fields. Then we declare a variable to hold the current index on the array. This variable will be reused when we start to continuously resize it on our looping from the record set result. Finally, we show how to assign the values from the record set to the array. A method to show the existance of an item in the array is also included in code snippet 2.
 | Code Snippet 1 |
 |
|
dim rsCarName,sqlCarName
reDim carInfoArr(3,0)
Dim currCarRecords : currCarRecords = 0
Set rsCarName = Server.CreateObject("ADODB.RECORDSET")
sqlCarName = "SELECT carID, carName, carMake, carModel FROM tbl_cars "
rsCarName.Open sqlCarName,strConnection
While Not rsCarName.EOF
currCarRecords = uBound(carInfoArr,2)
REDIM PRESERVE carInfoArr(3,currCarRecords+1)
carInfoArr(0,currCarRecords) = rsCarName("carID")
carInfoArr(1,currCarRecords) = rsCarName("carName")
carInfoArr(2,currCarRecords) = rsCarName("carMake")
carInfoArr(3,currCarRecords) = rsCarName("carModel")
rsCarName.MoveNext
Wend
rsCarName.close
set rsCarName = nothing
|
Now see below for a method to query our multidimensional array:
 | Code Snippet 2 |
 |
|
Function GetCarInfo( carID )
Dim i, found
found = false
dim retArray: retArray = Array("0","","","")
For i = 0 to UBound(carInfoArr,2)
if cstr(carInfoArr(0,i)) = cstr(carID) then
retVal(0) = carInfoArr(0,i)
retVal(1) = carInfoArr(1,i)
retVal(2) = carInfoArr(2,i)
retVal(3) = carInfoArr(3,i)
found = true
i = UBound(carInfoArr,2) 'break the loop
end if
Next
if found = false then
retArray = Array("0","No Info Available","N/A","N/A")
end if
GetCarInfo = retVal
End Function
|
Good luck!!.
|