require $_SERVER['DOCUMENT_ROOT']."/setcookies.php"; ?>
|
![]()
Method 2:
In Method 1, we are defining the entire array in one line. This method is useful for arrays that have a specific use, like the example above that only stores a list of months. In Method 2, we define values for each individual element on separate lines. This method can be used if you want to store elements from a record set as separate items of an array, or can be used in a loop to define values for each element, for example:
Dynamically re-sizing arraysLet us now look at dynamically resizing arrays. Why would you want to do this? Lets for example say you don't know how many items exist in a particular SELECT statement, but you want to store that data in an array. You will not be able to use the above declaration statements unless you get the count of the records and then declare the array. To avoid all that, lets look at the alternative. ASP comes with a function called REDIM which helps in re-sizing an array. Now, you must be asking, what happens to the data that already might exist in the array if I use the re-dim to resize an array? Well, if you were to use REDIM on its own the previous data in the array would be deleted. This is why the REDIM statement is equipped with another parameter, termed PRESERVE. When this parameter is used, the data in the array will be saved. Lets look at an example:
In the example above, the declaration of the array does not contain the number of items. All it contains is enough to tell ASP that it is an array that's being declared. We then use the REDIM statement to re-size the array to hold 20 items, and fill it in. Here is another way of writing the same example given above. Note that we use a new function here called Ubound(). Ubound returns the "upper bound" of an array, which only means the number of items already present in the array.
What we do in the for statement here is, get the number of elements in the array, use the REDIM PRESERVE function to resize the array to the number of elements plus one. The Preserve statement is essential here as we want to preserve the data that already exists.
Useful array functionsFunction: Ubound(arrayName) This function returns the "upper bound" index of an array, which basically is the number of elements in an array.
Function: Lbound(arrayName) This function returns the "lower bound" index of an array, that basically is the first element in an array. Use the same example above to test the Lbound() function (remember to change the function ubound to lbound though :) Function: Split(string, splitby) This function helps in converting a string into an array. Lets see an example using this function:
All we did was put up a string separated by comma's. We then used the split command to split the string using "," and store that into an array. Next we ran a for loop to display the results of an array, similar to the other examples at the beginning of this tutorial.
Finding elements in an arrayHere is a function that would return the "index" of the element in an array. The index is basically the position of the element, for example, 0 which stands for myArray(0).
Lets now see an example that uses the example above:
The second line directly gets the index of the element, passes it to the array, returning the value from the array.
Passing an array to another pageThere are various ways you could pass an array to another page. Lets see the three possibilities:
The first two are good, but are relatively more complex than the third way. Let us look at the 3rd possibility only because it's the simplest and most effective. Page1.asp
All we are doing is storing each individual element of the array as separate hidden fields in the form. Lets now see how to get them out on page2 and display them:
We have now covered the basics and a few advanced topics of arrays in ASP. Here are a few links that could help you further in the use of arrays:
................................................................................................... ![]() |
|
![]() |
|
About Stylus | India Advantage | Site Map
= date("F j, Y, g:i a"); ?> |
Clear about what you want? Click Here |