Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How would I do a multidimension array in Javascript? 3

Status
Not open for further replies.

altisdesign

Technical User
Apr 15, 2003
17
GB
Hi,

I have a MySQL table containing the unique ids of various members and their details. I need this table to be accessible to client side scripting (Javascript in this case). There is only two fields that I need to be able to access, the unique identifier and then their name, so that this can be loaded when required by Javascript. I assume I would have to do some kind of multi dimensional array, populating it when the page is loaded from the database using ASP. I can write the ASP statments to retrieve the data, but then once I have it in javascript would I for example be able to look up the name by searching for the unique id within the database. Is this possible?

Many Thanks in Advance
-Altis Design
 
>> I assume I would have to do some kind of multi
>> dimensional array....

No. You could, but you don't "Have to". You could design a Javascript class and have an array of the objects
Code:
function MyRec( nId, sName){
  this.id = nId;
  this.name = sName;
}

var myrecs = new Array();
<% while( !rs.eof){ %>
myrecs[myrecs.length] = new MyRec( <%=rs(&quot;ID&quot;)%>, &quot;<%=rs(&quot;NAME&quot;)%>&quot;);
<%    rs.MoveNext();
   }
%>

-pete


 
>I assume I would have to do some kind of multi dimensional array, populating it when the page is loaded from the database using ASP.

I have done it like this. In ASP create a variable and set it with your values in this format
Code:
aaData = &quot;[[1,'A'],[2,'B'],[3,'C']]&quot;
Then in JavaScript you can access it like this
Code:
eval(&quot;<%=aaData%>&quot;)[x][0] //*** = 1,2 or 3 ***
eval(&quot;<%=aaData%>&quot;)[x][1] //*** = 'A&quot;, 'B' or 'C' ***
(where the x in [x] is 1 for the first sub-array, 2 for the second sub-array...) This is an example, and you will be loading your aaData in a loop using data from your Database of course. Just use that format, and you should be good to go.

Hope that helps.



Good Luck! [thumbsup2]
WindUp
 
>but you don't &quot;Have to&quot;. You could design a Javascript class and have an array of the objects

I like palbano's answer better, especially for a large number of records (when I used my method, I knew it would only have a handfull of values.)



Good Luck! [thumbsup2]
WindUp
 
Thanks very much for that Pete, I will test that out later on tonight... One question, can I then retrieve based on the unique member id easily? This is so that the forms can pick users names out based on their ids.

Many Thanks for your help so far - I'm very grateful
-Altis Design
 
Altis,

Javascript arrays can be used as either &quot;map/dictionary&quot; with keys or as arrays with indecis but not both. So if you need both your better off using an array and developing your own &quot;find&quot; mechanism. If you don't need to use the array then you can use the &quot;dictionary&quot; feature of the Array class.

dictionary Array object
Code:
myrecs[&quot;<%=rs(&quot;NAME&quot;)%>&quot;] = new MyRec( <%=rs(&quot;ID&quot;)%>, &quot;<%=rs(&quot;NAME&quot;)%>&quot;);

-pete



 
I give a star to palbano for giving the answer I'd give.

I also use a trick to keep my array orderly and be able to reuse it elsewhere.

<script>

var records = new Array();

function MyRec(nId, sName){
this.id = records.length;
this.nId = nId;
this.name = sName;

this.getTR = function()
{
return &quot;<tr onclick='records[&quot; + this.id + &quot;].debug()'>&quot;
+ &quot;<td>&quot;
+ this.id
+ &quot;</td><td>&quot;
+ this.nId
+ &quot;</td><td>&quot;
+ this.sName
+ &quot;</td>&quot;
}

this.debug = function ()
{
alert(this.id + &quot;\n&quot; + this.nId + &quot;\n&quot; + this.sName)
}
}

Array.prototype.getTable = function()
{
var table = &quot;<table>&quot;;
for (var i = 0; i < this.length; i++)
{
table += this.getTR();
}
return table + &quot;</table>&quot;
}

Array.prototype.write = function()
{
document.write(this.getTable())
}

<%
while(!rs.eof)
{
%>
new MyRec(<%= rs(&quot;ID&quot;) %>, &quot;<%= rs(&quot;NAME&quot;) %>&quot;);
<%
rs.MoveNext();
}
%>

records.write()
</script>

How's that for organized coding? ;-)

Gary Haran
==========================
 
Gary, using [ i ] for indecis ?? What's up dude? LOL

-pete

 
I'm tired! :) LOL

<script>

var records = new Array();

function MyRec(nId, sName){
this.id = records.length;
this.nId = nId;
this.name = sName;

this.getTR = function()
{
return &quot;<tr onclick='records[&quot; + this.id + &quot;].debug()'>&quot;
+ &quot;<td>&quot;
+ this.id
+ &quot;</td><td>&quot;
+ this.nId
+ &quot;</td><td>&quot;
+ this.sName
+ &quot;</td>&quot;
}

this.debug = function ()
{
alert(this.id + &quot;\n&quot; + this.nId + &quot;\n&quot; + this.sName)
}
}

Array.prototype.getTable = function()
{
var table = &quot;<table>&quot;;
for (var i = 0; i < this.length; i++)
{
table += this.getTR();
}
return table + &quot;</table>&quot;
}

Array.prototype.write = function()
{
document.write(this.getTable())
}

<%
while(!rs.eof)
{
%>
new MyRec(<%= rs(&quot;ID&quot;) %>, &quot;<%= rs(&quot;NAME&quot;) %>&quot;);
<%
rs.MoveNext();
}
%>

records.write()
</script>

Gary Haran
==========================
 
I think I'll use the dictionary object as suggested. How would I then retrieve records?

Thanks
-Altis Design
 
Code:
var seek = &quot;altisdesign&quot;;
var oMyRec = myrecs[seek];
if( null != oMyRec)
  alert( &quot;Hello &quot; + oMyRec.name + &quot; your ID is: &quot; + oMyRec.id);

-pete

 
Just wanted to say thanks - havn't been back in a while but the code now works fine :) Many thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top