black 11-26-2003, 06:42 AM Howdy, i met some problem and could find a way to solve it...
the affairs is i want to store data extracted from database(mssql) to an array by looping, but finally i that array cant work properly, i checked everywhere but nothing helps, so plz heeeeeeeeeeeelp~~~~~~ my code is right below:
<%
var sql = "select * from products"
var rs = Server.CreateObject("Adodb.Recordset")
rs.open(sql, conn, 3, 3);
var classArr = new Array();
while(i<rs.recordcount)
{
classArr[i] = rs("title");
rs.moveNext();
i++;
}
Response.Write(classArr);
%>
btw, i rewrite the code into VBScript but cant get it work either, any help ???
Roelf 11-26-2003, 08:07 AM try
<%
var sql = "select * from products"
var rs = Server.CreateObject("Adodb.Recordset")
rs.open(sql, conn, 3, 3);
var classArr = new Array();
while(!rs.eof)
{
classArr[classArr.length] = rs("title");
rs.moveNext();
}
Response.Write(classArr);
%>
for stuffing the stuff in the array, i assume you tested the existance and contents of the recordset.
black 11-26-2003, 08:50 AM i tried that but it didnt work yet, i tested the content of the array and surprisingly found all elements are just the same value !!! what happened ??? :confused:
glenngv 11-26-2003, 08:55 AM why not use rs.getRows()?
it is used to copy records from a Recordset object into a 2D array.
jeskel 11-26-2003, 09:18 AM more infos on getRows here: http://www.stardeveloper.com/articles/display.html?article=2000080601&page=1
just in case :)
Oakendin 11-26-2003, 03:15 PM I didn't use a recordset to loop through the code, but it looks like you need a for loop when printing out the array. classArr[i] or a index for which position of the array you want printed classArr[0] or classArr[1]
<%
var i = 0;
var sql = "select * from products"
var rs = Server.CreateObject("Adodb.Recordset")
rs.open(sql, conn, 3, 3);
var classArr = new Array();
while(i<rs.recordcount)
{
classArr[i] = rs("title");
rs.moveNext();
i++;
}
for(j = 0; j < classArr.length; j++) {
Response.Write(classArr[j] + "<br>");
}
%>
----
Here's the code I tested with:
<%
//var sql = "select * from products"
//var rs = Server.CreateObject("Adodb.Recordset")
//rs.open(sql, conn, 3, 3);
var i = 0;
var classArr = new Array();
classArr[0] = "http://www.codingforums.com/images/icons/icon7.gif"
classArr[1] = "http://www.codingforums.com/images/icons/icon6.gif"
classArr[2] = "http://www.codingforums.com/images/icons/icon3.gif"
classArr[3] = "http://www.codingforums.com/images/icons/icon1.gif"
while(i<classArr.length)
{
Response.Write(classArr[i] + "<br>");
i++;
}
for(i = 0; i < classArr.length; i++) {
Response.Write(classArr[i] + "<br>");
}
%>
black 11-27-2003, 02:33 AM thanx Oakendin
the matter is i need to vlaue the array within looping because the length of the array can not be defined until runtime, and the problem is wrong value comes when i send data to the array through the loop, finally i just get an array consists of many value which can not be shown, the only go correct is the length except that i got everything useless... any ideas now ? :rolleyes:
glenngv 11-27-2003, 02:38 AM did you try rs.getRows()?
black 11-27-2003, 03:18 AM yes that cant help either, the problem is inside looping after which i cant get correct data with the array. :(
glenngv 11-27-2003, 05:33 AM try:
<%
var sql = "select * from products"
var rs = Server.CreateObject("Adodb.Recordset")
rs.open(sql, conn, 3, 3);
var classArr = rs.getRows();
var fieldCount = rs.Fields.Count;
rs.close();
Response.Write("<table border=\"1\">");
for (var i=0;i<classArr.length;i++)
Response.Write("<tr>");
for (var j=0;j<fieldCount;j++){
Response.Write("<td>" + classArr[i][j] + "</td>");
}
Response.Write("</tr>\n");
}
Response.Write("</table>");
%>
I'm not used to coding in JScript server-side so I'm not sure if the syntax I used for ADO API in JScript is correct or not.
black 11-27-2003, 05:40 AM thanx glenngv as your guess your code can work but... within that loop if i just Response.Write(rs("title")) every elements of the array all go nicely, however trouble comes if i try to pass values to that array like classArr[i] = rs("title"), this really confused me much, is there something wrong with database ? :confused:
glenngv 11-27-2003, 05:49 AM Let me ask first, does the code I posted work?
|
|