Products
Services
Contact Us

Scripts: Asp :: Asp arrays :: Library Article #16

Developer's Section

Creating a resizable multidimensional array in Classic ASP
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (1831)
Favorites:

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!!.


See other Scripts in Asp arrays

Submit Your Scripts:


System Message:
  • Your ASP & ASP.NET script has been sent. Please allow 48 hours for processing.


If you would like to have your ASP & ASP.NET scripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]