Looping More than Needed

Results 1 to 3 of 3

Thread: Looping More than Needed

  1. #1
    Join Date
    Dec 1969

    Default Looping More than Needed

    Hi!<BR><BR>Im using a join on a loop where, what is happening<BR>is ,in looping inside rs1 ,im getting each value 4 times instead<BR>of 1 (4 being the number of records in column &#039;type&#039; )<BR><BR>&#060;%<BR>rs.open "select * from table1 where m_id=&#039;XS101&#039;"<BR> <BR> i= 0 <BR> for each item in rs.fields<BR> if (i &#062; 0)then <BR> if (item.value &#060;&#062; "NULL") then<BR> Response.Write (item.value) & "&#060;hr&#062;"<BR> sq2= "select brief_desc from table2 as A ,table3 as B WHERE A.type=B.type AND m_id=&#039;XS101&#039; AND B.type=&#039;" & (item.value) & "&#039;"<BR> <BR> rs1.open sq2<BR> while not rs1.eof<BR> response.write rs1("brief_desc") & "<BR>"<BR> rs1.movenext<BR> wend<BR> rs1.close<BR> <BR> end if<BR> end if <BR> i = i + 1 <BR> next <BR> rs.close<BR> %&#062;<BR><BR>The output is coming like this:<BR><BR>AA<BR><BR>x1<BR>x1<BR>x1<BR>x1<BR>x2< BR>x2<BR>x2<BR>x2<BR>x2<BR>x3<BR>x3<BR>x3<BR>x3<BR ><BR>BB<BR><BR>y1<BR>y1<BR>y1<BR>y2<BR>y2<BR>y2<BR >y3<BR>y3<BR>y3<BR><BR>CC<BR><BR>z1<BR>...<BR>.... <BR><BR>BUT THE CORRECT OUTPUT SHUD BE LIKE THIS :<BR><BR>AA<BR><BR>x1<BR>x2<BR>x3<BR><BR>BB<BR><BR >y1<BR>y2<BR>y3<BR><BR>CC<BR>z1<BR>...<BR>....<BR> <BR>Im not able to figure out ,plz help in this code<BR>thanks !!<BR>

  2. #2
    Join Date
    Dec 1969

    Default You DO NOT want to do this.

    1) (item.value &#060;&#062; "NULL") ---- This is saying if the value is &#060;&#062; then the word "NULL" It isn&#039;t actually checking for Null values. That would be (item.value &#060;&#062; Null) Unless you have the word "Null" in your database every one of your records will pass that logic test. <BR><BR>2)YOU DO NOT WANT TO QUERY INSIDE A LOOP. THIS IS A VERY BAD THING TO DO. DO.NOT.DO.IT. <BR><BR>Instead, query for all the records that you want BEFORE the loop in ONE query, then do a nested loop to display them correctly. Here is an example of a nested loop using an array. <BR><BR>arrFood(0,0) = "Breakfast"<BR>arrFood(1,0) = "Eggs"<BR>arrFood(0,1) = "Breakfast"<BR>arrFood(1,1) = "Pancakes"<BR>arrFood(0,2) = "Dinner"<BR>arrFood(1,2) = "Steak<BR>arrFood(0,3) = "Dinner"<BR>arrFood(1,3) = "Chicken"<BR><BR>arrMeal(0) = "Breakfast"<BR>arrMeal(1) = "Lunch"<BR>arrMeal(2) = "Dinner"<BR><BR>For i = 0 to ubound(arrMeal)<BR><BR> For ii = 0 to ubound(arrFood,2)<BR> If arrFood(0,ii) = arrMeal(i) Then<BR> Response.Write(arrFood(1,ii)<BR> End If<BR> Next<BR>Next<BR><BR>This will loop through the list of meals, then it will loop through all the food items and display the one that falls under the appropriate meal. I hard-coded the array at the top, but that could just have easily came from a database table. <BR>

  3. #3
    Join Date
    Dec 1969

    Default Minor mistake...

    Shane wrote:<BR> (item.value &#060;&#062; Null) <BR>No. You need to do<BR> NOT ( item.value IS Null )<BR><BR>If you test a string against NULL, no matter whether you use =, &#060;&#062;, &#062;, &#062;=, &#060;, or &#060;= the result of the test is alwasy...ready for this?...NULL!!! So doing<BR> (item.value &#060;&#062; Null)<BR>will always give you NULL as the test result. Which is useless, of course.<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