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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populating array from database

Status
Not open for further replies.

ashaig

Programmer
Apr 9, 2001
26
GB
I'm trying to populate an array. Instead of slots looking like "Accomodation","Ceremony","Transport" etc. they are being populated as below the code. Any kind person who can help? (I suspect I should build up a delimited string then 'split' it...)

var sDesc;
var sId;
var sM1;
var sM2;
var scatDesc = new Array(12);
var scatId = new Array(12);
var sMover1 = new Array(12);
var sMover2 = new Array(12);
var i = 0;


while (!(loRS.Eof))
{
sDesc = loRS("Description");
sId = loRS("CatId");
sM1 = loRS("Mover1");
sM2 = loRS("Mover2");

scatDesc = sDesc;
Response.Write(scatDesc);
Response.Write(i);

Response.Write(&quot;<br>&quot;);

scatId = sId;
sMover1 = sM1;
sMover2 = sM2;

loRS.MoveNext();
i++;
}
loRS = null;

//PROBLEM

//RESULT OF &quot;Response.Write(scatDesc)&quot; below:

//Accommodation,,,,,,,,,,,
//Ceremony,Ceremony,Ceremony,Ceremony,Ceremony...etc
//Transport,Transport,Transport,Transport,Transport....etc
 
scatDEsc, scatid .... are ARRAYS
so you have to specify which index you want to write the value for
if you did but i didn't see (because of the tgml processing, please think of UNCHECKING the box before posting) - then how are you populating the arrays ? i don't see it in the piece of code you posted
 
Hi Iza,

Sorry about unchecked box - I'm a newbie! Yes, it was a typo leaving the index out. I'll put the code below - hope you can see it this time.

var sDesc;
var sId;
var sM1;
var sM2;
var scatDesc = new Array(12);
var scatId = new Array(12);
var sMover1 = new Array(12);
var sMover2 = new Array(12);
var i = 0;


while (!(loRS.Eof))
{
sDesc = loRS(&quot;Description&quot;);
sId = loRS(&quot;CatId&quot;);
sM1 = loRS(&quot;Mover1&quot;);
sM2 = loRS(&quot;Mover2&quot;);

scatDesc = sDesc;
Response.Write(scatDesc);
Response.Write(i);

Response.Write(&quot;<br>&quot;);

scatId = sId;
sMover1 = sM1;
sMover2 = sM2;

loRS.MoveNext();
i++;
}
loRS = null;

//PROBLEM

//RESULT OF &quot;Response.Write(scatDesc)&quot; below:

//Accommodation,,,,,,,,,,,
//Ceremony,Ceremony,Ceremony,Ceremony,Ceremony...etc
//Transport,Transport,Transport,Transport,Transport....etc
 
what you wrote is in red and my comments in black :
sDesc = loRS(&quot;Description&quot;); i don't know what loRS does but the retrun value of loRS(&quot;Des..&quot;) is stored in sDesc ..
...
scatDesc[ i ] = sDesc;
the same value as above is now also stored at the i-th position in the scatDesc Array
Response.Write(scatDesc); i don't know which type is the Response object, but you are NOT writing a value of the array, you're likely writing its name
Response.Write(i); you're writing 0 the 1st time, then 1, then ...
Response.Write(&quot;<br>&quot;);
...

so you should get in Response :
name_of_the_array 0
name_of_the_array 1
...
-> what is Response ? how does its write method works ? double check it and make sure it's how you want t to work (for example, Response.write(&quot;test&quot;) and alert(Response))
-> how does the loRS function works ? does it return the correct value ? alert() what it retruns to make sure it's what you expect - (sDesc = loRS(&quot;Description&quot;); then alert(sDesc) to &quot;see&quot; its value)
 
Response and loRS are Active Server Objects used in writing web-pages. They don't impact on the problem. Response.Write(scatDesc) is writing out the array at the bottom, which seems to have all slots overwritten each time through the loop. I use Response as a debug aid, but I'll try the alert function too. I can't help feeling that its my understanding of how to fill the slots on the array which is at fault, the only other time I've used JS arrays is when splitting up a delimited string, and that works fine.

Thanks for looking at my problem!!
 
Hey ashaig,
I am more familiar with Acroform JS, but let me see if I can get what you are trying to do...
You have an array 'scatDesc', which has a number of elements indexed by 'i' (i=0,1,2,3 etc).
In your 'while' loop you are giving each element the value of sDesc.

2 questions - will 'IoRS(&quot;Description&quot;)' give a different value each time thro' the loop? and what are you trying to do with 'Response.Write(scatDesc);'...?

'scatDesc' is the array - i.e. the whole array, so I suspect that having this line in your 'while' loop is going to be writing out the whole array each time thro' - is this what you want?
Or do you really want something like
'Response.Write(scatDesc)' - which will write that particular element of the array?

Hope this helps.
Jub
 
Hi Jub,

Response.Write is just a debug to see what is going on.
'loRS(&quot;Description&quot;)' is the RecordSet of a call to the database which brings back all rows on SQLServer matching your criteria.
&quot;loRS.MoveNext();&quot;
moves the next row into 'IoRS(&quot;Description&quot;)'
I am aware that I'm printing out the whole array each time, but I'm expecting each subsequent slot to be filled with a new value. I'm beginning to suspect its a Active Server/JavaScript mismatch, as no-one seems to think I'm trying to fill the array wrongly!

Thanks for your interest!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top