Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Nov 2003
    Location
    nowhere
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    save data to array problem, very urgent !!!

    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:
    Code:
    <%
    	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 ???
    Never end on learning~

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    try
    Code:
    <%
    	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.
    I am the luckiest man in the world

  • #3
    New Coder
    Join Date
    Nov 2003
    Location
    nowhere
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ???
    Never end on learning~

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,045
    Thanks
    0
    Thanked 251 Times in 247 Posts
    why not use rs.getRows()?
    it is used to copy records from a Recordset object into a 2D array.

  • #5
    Regular Coder
    Join Date
    Aug 2003
    Posts
    565
    Thanks
    0
    Thanked 0 Times in 0 Posts
    more infos on getRows here: http://www.stardeveloper.com/article...0080601&page=1
    just in case

  • #6
    New Coder
    Join Date
    Jul 2002
    Location
    Los Angeles
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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]

    Code:
    <%
    
    	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:
    Code:
    <%
    	//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>");
    	}
    %>
    Last edited by Oakendin; 11-26-2003 at 03:21 PM.

  • #7
    New Coder
    Join Date
    Nov 2003
    Location
    nowhere
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ?
    Never end on learning~

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,045
    Thanks
    0
    Thanked 251 Times in 247 Posts
    did you try rs.getRows()?

  • #9
    New Coder
    Join Date
    Nov 2003
    Location
    nowhere
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes that cant help either, the problem is inside looping after which i cant get correct data with the array.
    Never end on learning~

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,045
    Thanks
    0
    Thanked 251 Times in 247 Posts
    try:
    Code:
    <%
    	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.

  • #11
    New Coder
    Join Date
    Nov 2003
    Location
    nowhere
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ?
    Never end on learning~

  • #12
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,045
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Let me ask first, does the code I posted work?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •