Help with a nested loop

Discussion in 'C#' started by Tobbe, Feb 14, 2008.

  1. #1
    Need some help with a nested loop.

    This one (my example code below) prints out a correct 3 column HTML table with the recordset with proper opening and closing HTML tags AS LONG as the GRUOP BY clause is set by the topcategory Db table.

    I think I may need a additional loop withing the existing one to handle the one-to-many database query without messing up the HTML table cells and rows. I have tried to find a way to count posts from the middlecategory query, and this might be a part of the solution.

    EXAMPLE CODE:

    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 "
    RS.Open SQL,Conn

    Cols = 3

    Response.Write("<table width='570' border='1'>")

    If Not RS.EOF then

    Do Until RS.EOF
    Response.Write("<tr>")

    For i = 1 To Cols

    If RS.EOF then
    Response.Write("<td width='190'>xx")
    Else

    Response.Write("<td width='190'>")

    ' I NEED START OF LOOP FROM HERE

    TopCat = RS("topcategory")
    If LastTopCat <> TopCat Then
    Response.Write(""&RS("topcategory") &"<br>")
    Response.Write("-----------------------------<br>")
    LastTopCat = TopCat
    End If

    Response.Write(RS("middlecategory"))

    ' END OF LOOP TO HERE

    RS.MoveNext

    End If
    Response.Write("</td>")
    Next

    Response.Write("</tr>")

    Loop
    End If
    Response.Write("</table>")



    thanks in advance
    Torbjorn
     
    Tobbe, Feb 14, 2008 IP
  2. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm a little confused by what you are looking for, so this may not be what you want.

    But to do three columns, this is a simple method

    
    cntr = 1
    
    start DB loop
    if cntr = 1 then
    response.write("<tr>")
    end if
    
    response.write("<td>")
    response.write("cell content goes here")
    response.write("</td>")
    
    if cntr = 3 then
    response.write("</tr>")
    cntr = 0
    end if
    
    cntr = cntr + 1
    
    end db loop
    
    
    Code (markup):
     
    imvain2, Feb 15, 2008 IP