954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Javascript array from sql query

//this is some garbage i put in the page to make sure the values were indeed loading the recordset, indeed they are
<%
while (keywords.next()) {
out.println("'");
out.println(keywords.getString("term"));
out.println("'");
out.println(",");

}
keywords.close();
%>

//this is the text box that as a user types in something, based up an array, it will auto-complete similar to Google Suggest's functionality

meggsie
Newbie Poster
2 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

Why not:

//this is some garbage i put in the page to make sure the values were indeed loading the recordset, indeed they are

var custom3 = new Array(<%
while (keywords.next()) {
out.println("'");
out.println(keywords.getString("term"));
out.println("'");
out.println(",");
}
keywords.close();
%>);

or similar...

alpha_foobar
Junior Poster
182 posts since May 2005
Reputation Points: 20
Solved Threads: 5
 

i have tried that, but it doesn't work. does anyone else know how i might get a recordset from a sql query into an array in JSP or javascript?

thanks
megan

meggsie
Newbie Poster
2 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

I assume your only question is how to build your javascript line that defines the array, but instead of hard-coding the array items, you want them populated from a SQL query?

If this is the case, then this is a server-side question. And that depends on the language you are using. And this would be the wrong forum.

My suggestion is to iterate your databse recordset building a string where each time you append a comma, a single tick, the value, and another single tick. At the end of the loop, simply strip off the beginning comma. (Look at the substr command or similar in your language of choice.)

Then all you do is write your string into the javascript line as you are outputting the page.

Troy
Posting Whiz
362 posts since Jun 2005
Reputation Points: 36
Solved Threads: 6
 

Troy is absolutely correct. From your code posting, I can see that your server-side language is JSP. JavaScript is client-side. There is nothing you can do CLIENT-SIDE to build an array from data that exists on the SERVER.

Therefore I'm moving this thread to the JSP forum. I wish you well.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You