Click to See Complete Forum and Search --> : How is a recordset loaded into a Java Script array?


Pelle
03-25-2005, 02:40 PM
I need to load a recordset into a Java Script array called "My_JS_Array" but don't know how to achieve that. The real problem for me is how I can increase the index of the array evertime the recordset moves to the next record. I hope someone knows and want to tell me how that should be done (and that my thread is placed in the correct forum :) )

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>

<html>
<head>
<script language="javascript" type="text/javascript">
var My_JS_Array = new Array();
</script>
</head>

<body>

<%
SQL = "SELECT * FROM MyTable"
rs.open SQL, con

Do while not rs.EOF
' Assume that the recordset now contains 4 records with these values (value0, value1, value2 and value3).
' How can this recordset be loaded into My_JS_Array. The JS-array should adapt dynamically, that is
' the array index should be increased by one every time the recordset loops (index 0 should store "value0" etc.).
%>



<%
rs.movenext
Loop
con.close
%

</body>
</html>

phpnovice
03-25-2005, 04:05 PM
I need to load a recordset into a Java Script array called "My_JS_Array" but don't know how to achieve that.
I would not try to increment an index. Instead, use the following method:

<script language="javascript" type="text/javascript">
<!--//
var My_JS_Array = new Array(
<%
SQL = "SELECT * FROM MyTable"
rs.open SQL, con
Do while not rs.EOF
Response.Write " " & rs.Fields("field_name").Value & "," & vbCrLf
rs.movenext
Loop
rs.close
%>
"END"
); // no comma after last entry above
//-->
</script>

Now, the above presumes a numeric value for the field in question. If this field is a string value, then just code that one line as follows:

Response.Write " '" & rs.Fields("field_name").Value & "'," & vbCrLf

Lastly, you'll notice that I've inserted a dummy end-of-array entry. Your JavaScript code will have to take this into consideration. There is another way to arrange this code if you really want to eliminate that dummy entry. Just ask.