Looping through an array

Results 1 to 4 of 4

Thread: Looping through an array

  1. #1
    Jason Binns Guest

    Default Looping through an array

    Hi There,<BR><BR>Looking for some advice...<BR><BR>I&#039m writing an ASP page that does a select on a database and then loops through each record displaying a background colour inside a table cell for each field. <BR><BR>I&#039ve got the select working, taken the results and put them into an array. What I now want to do is loop through the array assigning a value to a variable using the code below:<BR><BR> If <BR> approved = "yes" and Display(0) = "yes" then <BR> dd01 = "#000000"<BR> elseIf <BR> approved = "no" and Display(0) = "yes" then<BR> dd01 = "#c0c0c0"<BR> else<BR> dd01 = "#ffffff"<BR> end If<BR><BR>Any help would be appreciated as my O&#039Reilly is about 65 miles away at home!<BR><BR>Cheers<BR><BR>J.<BR>--<BR>

  2. #2
    Franco Guest

    Default RE: Looping through an array

    Have you tried a For...Next or a For Each loop?<BR><BR>Alternatively, you could reference the recordset directly:<BR><BR>Do Until objRs.EOF<BR>If objRs("Colour") = "Pink" Then<BR>...<BR>End If<BR>objRs.MoveNext<BR>Loop<BR><BR>Good luck!<BR><BR>Franco<BR><BR>

  3. #3
    Jason Binns Guest

    Default RE: Looping through an array

    Hi Franco,<BR><BR>I&#039ve been looking at the following for loop, can&#039t seem to get it to work though:<BR><BR>&#060;%<BR>do while not rs.eof <BR><BR>Dim num<BR>Dim Display(30)<BR> Display(0) = rs("d01")<BR> Display(1) = rs("d02") <BR> Display(2) = rs("d03")<BR> Display(3) = rs("d04")<BR> Display(4) = rs("d05")<BR> Display(5) = rs("d06")<BR><BR> &#039 there are 30 items in the array snipped them to save <BR> &#039 space...<BR><BR> id = rs("id")<BR> approved = rs("approved")<BR> num = "0"<BR> <BR>For each num in Display <BR> If <BR> approved = "yes" and Display(num) = "yes" then<BR> d" & "num" & "++1 = "#000000"<BR> elseIf <BR> approved = "no" and Display(num) = "yes" then<BR> d" & "num" & "++1 = "#c0c0c0"<BR> else<BR> d" & "num" & "++1" = "#ffffff"<BR><BR>end If<BR>Next <BR>%&#062;<BR><BR>Then the html for the recordset.<BR><BR>Can you see what I&#039m trying to do I want to count up through the array and output 30 (31) values... Been at this too long :(<BR><BR>J.<BR>--<BR><BR>

  4. #4
    Join Date
    Dec 1969

    Default RE: Looping through an array

    Jason<BR>A couple of things, assume table has fields for id, num1, num2 & approved only to simplify:<BR><BR>&#060;%<BR>cstID = 0<BR>cstNum1 = 1<BR>cstNum2 = 2<BR>cstApproved = 3<BR>Dim i<BR><BR>Sample_DSN = "DSN=Sample_DSN;uid=sa;pwd="<BR>Set Sample_Connection = server.createobject("adodb.connection")<BR>Sample_ Connection.Open Sample_DSN<BR>Sample_SQL = "Select id, approved from mytable where whatever=1"<BR>Set rsSample = Sample_Connection.Execute(Sample_SQL)<BR>Samples = rsSample.GetRows<BR>rsSample.Close<BR>Set rsSample = Nothing<BR>Sample_Connection.Close<BR>Set Sample_Connection = Nothing<BR>i = 0<BR>While i &#060;= UBound(Samples)<BR> If Samples(cstApproved,i) = "Yes" And Samples(cstNum1)="Yes" Then<BR> Color = Red<BR> ElseIf<BR> Color = Blue<BR> Else<BR> Color = Black<BR> End If<BR> i = i + 1<BR>Wend<BR>%&#062;<BR><BR>Note the following set constants for field names into array ( zero based), use GetRows Ado command then close the recordset and connection to minimize connection time to db. Use the Function UBound(ArrayName) to determine length of array.<BR><BR>Good Luck<BR>mailtot:gary@mennies.com<BR><BR>PS If I am missing the point post all of your code or e-mail it to me.<BR><BR><BR><BR>

Posting Permissions

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