Nested loop

Thread Solved

Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Nested loop

 
0
  #11
Feb 14th, 2008
Okay here is the work around. And sorry, I left out the </td> last time, that's why it looks a bit funky.
  1. SQL = "SELECT DISTINCT TC.topcategory,MC.middlecategory "&_
  2. "FROM tbtopcategory TC,tbmiddlecategory MC,tbconnectcategory CC "&_
  3. "WHERE TC.topcategoryID = CC.topcategorylink "&_
  4. "AND MC.tbmiddlecategoryID = CC.middlecategorylink " &_
  5. "GROUP BY TC.topcategory "
  6. RS.Open SQL,Conn
  7.  
  8. Cols = 3
  9.  
  10. Response.Write("<table width='570' border='1'>")
  11.  
  12. If Not RS.EOF then
  13. Do Until RS.EOF
  14. Response.Write("<tr>")
  15.  
  16. For i = 1 To Cols
  17. If RS.EOF then
  18. Response.Write("<td width='190'>xx")
  19. Else
  20. Response.Write("<td width='190'>")
  21.  
  22. TopCat = RS("topcategory")
  23.  
  24. If LastTopCat <> TopCat or Then
  25. Response.Write(RS("topcategory") & "<br>")
  26. Response.Write("-----------------------------<br>")
  27. LastTopCat = TopCat
  28. Response.Write(RS("middlecategory"))
  29. End If
  30.  
  31. Response.Write("</td>")
  32. RS.MoveNext
  33. End If
  34. Next
  35.  
  36. Response.Write("</tr>")
  37. loop
  38. End If
  39.  
  40. Response.Write("</table>")
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 174
Reputation: TobbeK is an unknown quantity at this point 
Solved Threads: 3
TobbeK TobbeK is offline Offline
Junior Poster

Re: Nested loop

 
0
  #12
Feb 14th, 2008
There is some error, i get back in short
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 174
Reputation: TobbeK is an unknown quantity at this point 
Solved Threads: 3
TobbeK TobbeK is offline Offline
Junior Poster

Re: Nested loop

 
0
  #13
Feb 14th, 2008
I am really grateful for all the help I can get. This is a tricky coding we are doing.

This is With the GROUP BY clause closed, so all of the middlecats can be received.

Here is the Screen

SQL = "SELECT DISTINCT TC.topcategory,MC.middlecategory "&_
"FROM tbtopcategory TC,tbmiddlecategory MC,tbconnectcategory CC "&_
"WHERE TC.topcategoryID = CC.topcategorylink "&_
"AND MC.tbmiddlecategoryID = CC.middlecategorylink " '&_
'"GROUP BY TC.topcategory ASC"
Attached Thumbnails
table_7.jpg  
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Nested loop

 
0
  #14
Feb 14th, 2008
I left in the "or" command with no argument.
  1. SQL = "SELECT DISTINCT TC.topcategory,MC.middlecategory "&_
  2. "FROM tbtopcategory TC,tbmiddlecategory MC,tbconnectcategory CC "&_
  3. "WHERE TC.topcategoryID = CC.topcategorylink "&_
  4. "AND MC.tbmiddlecategoryID = CC.middlecategorylink " &_
  5. "GROUP BY TC.topcategory "
  6. RS.Open SQL,Conn
  7.  
  8. Cols = 3
  9.  
  10. Response.Write("<table width='570' border='1'>")
  11.  
  12. If Not RS.EOF then
  13. Do Until RS.EOF
  14. Response.Write("<tr>")
  15.  
  16. For i = 1 To Cols
  17. If RS.EOF then
  18. Response.Write("<td width='190'>xx")
  19. Else
  20. Response.Write("<td width='190'>")
  21.  
  22. TopCat = RS("topcategory")
  23.  
  24. If LastTopCat <> TopCat Then
  25. Response.Write(RS("topcategory") & "<br>")
  26. Response.Write("-----------------------------<br>")
  27. LastTopCat = TopCat
  28. Response.Write(RS("middlecategory"))
  29. End If
  30.  
  31. Response.Write("</td>")
  32. RS.MoveNext
  33. End If
  34. Next
  35.  
  36. Response.Write("</tr>")
  37. loop
  38. End If
  39.  
  40. Response.Write("</table>")
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 174
Reputation: TobbeK is an unknown quantity at this point 
Solved Threads: 3
TobbeK TobbeK is offline Offline
Junior Poster

Re: Nested loop

 
0
  #15
Feb 14th, 2008
Yes i noticed the Or argument before and take it away, but it is the same output as my last post.

Is there anyway to count the middlecategory posts for each of the topcategory, flag the last middlecategory and write a </tr> if the last post has been reached. I have tried that but without success.

What I am trying to do here is (a simple) small directory link table (like Yahoo). With Topcategory and undercategory links (URLs) Nothing fancy, even if it is trickier that first thought.
Last edited by TobbeK; Feb 14th, 2008 at 3:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Nested loop

 
1
  #16
Feb 14th, 2008
Okay, then this is the way I would do it if you don't mind me rewriting:
  1. Response.Write("<table width='570' border='1'>")
  2.  
  3. Do While Not RS.EOF
  4. Response.Write("<tr>")
  5.  
  6. For i = 1 To 3
  7. If Not RS.EOF Then
  8. Response.Write("<td width='190'>")
  9.  
  10. TopCat = RS("topcategory")
  11. MiddleCat = RS("middlecategory")
  12.  
  13. If TopCat <> "" Then
  14. Response.Write(RS("topcategory") & "<br>")
  15.  
  16. If MiddleCat <> "" Then
  17. Response.Write("-----------------------------<br>")
  18. Response.Write(RS("middlecategory"))
  19. End If
  20. End If
  21.  
  22. TopCat = ""
  23. MiddleCat = ""
  24.  
  25. Response.Write("</td>")
  26. RS.MoveNext
  27. Else
  28. Response.Write("<td width='190'></td>")
  29. End If
  30. Next
  31.  
  32. Response.Write("</tr>")
  33. Loop
  34.  
  35. Response.Write("</table>")
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 174
Reputation: TobbeK is an unknown quantity at this point 
Solved Threads: 3
TobbeK TobbeK is offline Offline
Junior Poster

Re: Nested loop

 
0
  #17
Feb 14th, 2008
It's getting better. But there still is some repeating

Check the attach.

Originally Posted by SheSaidImaPregy View Post
Okay, then this is the way I would do it if you don't mind me rewriting:
  1. Response.Write("<table width='570' border='1'>")
  2.  
  3. Do While Not RS.EOF
  4. Response.Write("<tr>")
  5.  
  6. For i = 1 To 3
  7. If Not RS.EOF Then
  8. Response.Write("<td width='190'>")
  9.  
  10. TopCat = RS("topcategory")
  11. MiddleCat = RS("middlecategory")
  12.  
  13. If TopCat <> "" Then
  14. Response.Write(RS("topcategory") & "<br>")
  15.  
  16. If MiddleCat <> "" Then
  17. Response.Write("-----------------------------<br>")
  18. Response.Write(RS("middlecategory"))
  19. End If
  20. End If
  21.  
  22. TopCat = ""
  23. MiddleCat = ""
  24.  
  25. Response.Write("</td>")
  26. RS.MoveNext
  27. Else
  28. Response.Write("<td width='190'></td>")
  29. End If
  30. Next
  31.  
  32. Response.Write("</tr>")
  33. Loop
  34.  
  35. Response.Write("</table>")
Attached Thumbnails
table_8.jpg  
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Nested loop

 
0
  #18
Feb 14th, 2008
That's because I didn't keep the If TopCat is the same as the LastTopCat. I don't know how your DB is set up, so here's the good code:
  1. Response.Write("<table width='570' border='1'>")
  2.  
  3. Do While Not RS.EOF
  4. Response.Write("<tr>")
  5.  
  6. For i = 1 To 3
  7. If Not RS.EOF Then
  8. Response.Write("<td width='190'>")
  9.  
  10. TopCat = RS("topcategory")
  11. MiddleCat = RS("middlecategory")
  12.  
  13. If TopCat <> "" and TopCat <> LastTopCat Then
  14. Response.Write(RS("topcategory") & "<br>")
  15. LastTopCat = TopCat
  16.  
  17. If MiddleCat <> "" Then
  18. Response.Write("-----------------------------<br>")
  19. Response.Write(RS("middlecategory"))
  20. End If
  21. End If
  22.  
  23. TopCat = ""
  24. MiddleCat = ""
  25.  
  26. Response.Write("</td>")
  27. RS.MoveNext
  28. Else
  29. Response.Write("<td width='190'></td>")
  30. End If
  31. Next
  32.  
  33. Response.Write("</tr>")
  34. Loop
  35.  
  36. Response.Write("</table>")
However, your "DISTINCT" should have picked through this so there are no repetitions.
Last edited by SheSaidImaPregy; Feb 14th, 2008 at 4:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Nested loop

 
0
  #19
Feb 14th, 2008
Try changing your SQL to this before changing the above code.
  1. SQL = "SELECT DISTINCT(TC.topcategory), MC.middlecategory "&_
  2. "FROM tbtopcategory TC,tbmiddlecategory MC,tbconnectcategory CC "&_
  3. "WHERE TC.topcategoryID = CC.topcategorylink "&_
  4. "AND MC.tbmiddlecategoryID = CC.middlecategorylink " &_
  5. "GROUP BY TC.topcategory "
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 174
Reputation: TobbeK is an unknown quantity at this point 
Solved Threads: 3
TobbeK TobbeK is offline Offline
Junior Poster

Re: Nested loop

 
0
  #20
Feb 14th, 2008
Nope the last one did not work (same error as two posts ago)

I cut the source of your code before the last one.

I looks good, but the topcategories is repeated with the middlecategories.

On the LEFT side you see the topcategories and on the RIGHT the middlecategories.
The topcategories should be written only once.

<table width='570' border='1'>
<tr>
<td width='190'>Bil, Båt & Motor<br>-----------------------------<br>Bilservice</td>
<td width='190'>Bil, Båt & Motor<br>-----------------------------<br>Bildelar & Biltillbehör</td>
<td width='190'>Butiker & Shopping<br>-----------------------------<br>Hälsa & Skönhet</td>
</tr>
<tr>
<td width='190'>Butiker & Shopping<br>-----------------------------<br>Bygg & Trädgård</td>
<td width='190'>Butiker & Shopping<br>-----------------------------<br>Hus, Hem & Trädgård</td>
<td width='190'>Butiker & Shopping<br>-----------------------------<br>Film, Musik & Spel</td>
</tr>
<tr>
<td width='190'>Butiker & Shopping<br>-----------------------------<br>Mobilt & Telefoni</td>
<td width='190'>Butiker & Shopping<br>-----------------------------<br>Kläder, Mode & Skor</td>
<td width='190'>Butiker & Shopping<br>-----------------------------<br>Glasögon & Kontaktlinser</td>
</tr>
<tr>
<td width='190'>Butiker & Shopping<br>-----------------------------<br>Hemelektronik & Foto</td>
<td width='190'>Butiker & Shopping<br>-----------------------------<br>Företag & Kontor</td>
<td width='190'>Data, IT & Telekommunikation<br>-----------------------------<br>Datorer & Nätverk</td>
</tr>
<tr>
<td width='190'>Data, IT & Telekommunikation<br>-----------------------------<br>Mobilt & Telefoni</td>
<td width='190'>Data, IT & Telekommunikation<br>-----------------------------<br>Internettjänster</td>
<td width='190'>Ekonomi & Juridik<br>-----------------------------<br>Advokat- & Juristbyråer</td>
</tr>
<tr>
<td width='190'>Ekonomi & Juridik<br>-----------------------------<br>Administration & Redovisning</td>
<td width='190'>Ekonomi & Juridik<br>-----------------------------<br>Bank, Finans & Egendom</td>
<td width='190'>Industri & Transport<br>-----------------------------<br>Åkeri & Transporttjänster</td>
</tr>
<tr>
<td width='190'>Marknadsföring & Media<br>-----------------------------<br>Reklam & Annons</td>
<td width='190'>Marknadsföring & Media<br>-----------------------------<br>Internetmarknadsföring</td>
<td width='190'>Marknadsföring & Media<br>-----------------------------<br>Design & Formgivning</td>
</tr>
<tr>
<td width='190'>Marknadsföring & Media<br>-----------------------------<br>PR & Kommunikation</td>
<td width='190'>Sport & Fritid<br>-----------------------------<br>Äventyr & Naturupplevelser</td>
<td width='190'>xx</td>
</tr>
</table>
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for ASP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC