views:

164

answers:

2

Hi there,

I am trying to create a menu using ASP (I have never used ASP before, im a PHP man) using values stored in a database.

basically the html layout i want is as such:

<ul>
 <li>
  <ul class="sub-menu">
   <li class="sub-menu-li">Test</li>
  </ul>
 </li>
</ul>

I need to loop around the root menu items rs("AD_Level") which is equal to 0 for root objects, then inside that loop, lop around anything that has the same parent id eg if the current record is AD_Level =0 and AD_Parent=5 then loop around all items with AD_Parent 5 and AD_Level != 0 and insert the values into html and so on and so forth.

Please help! I am struggling with a new language and cannot see a way to do this without losing sanity

Edit (Extracted from Comment by OP)

while not rsAdmin.eof
  sPar = rsAdmin("ad_parent"
  if rsAdmin("AD_Level")=0 then
    while not rsAdmin2.eof
      if rsAdmin2("AD_Level")<>0 and rsAdmin2("ad_parent")=sPar and rsAdmin2("AD_Sec_Level")=>2 then
        response.write rsAdmin("AD_Menu")
      end if
      rsAdmin2.movenext
    wend
  end if 
  '' # if not rsAdmin.eof then sPar=rsAdmin("AD_parent") rsAdmin.movenext
wend

that is my code

A: 

I have edited my answer (again). See if it helps.

<%
    '' use RECORDSET.Filter property instead of VBScript code

    rsAdmin1.Filter = "AD_Level = 0"

    do until rsAdmin1.EOF

        '' use RECORDSET.Filter property instead of VBScript code

        rsAdmin2.Filter = "AD_Level <> 0 AND AD_Parent = " & rsAdmin1( "AD_Parent" ) & " AND AD_Sec_Level >= 2"

        Response.Write "<ul class=""sub-menu"">"

        do until rsAdmin2.EOF
            Response.Write "<li class=""sub-menu-li"">"
            Response.Write rsAdmin1( "AD_Menu" )
            Response.Write "</li>"
            rsAdmin2.movenext

        loop

        Response.Write "</ul>"

        rsAdmin1.movenext

    loop
%>
Salman A
A: 

Surely this is an obvious case for using recursion, just providing psuedo-code below since I can't really understand your menu structure very well from your code so I've not attempted to put it directly into ASP code:

For Each MenuItem at with Level=0
    Display the Menu Text (if applicable)
    Call GenerateSubMenu(MenuItem.ID)
Next

Function GenerateSubMenu(ID)
    For Each MenuItem with Parent=ID
        Display the Menu Text (if applicable)
        Call GenerateSubMenu(MenuItem.ID)
    Next
End Function
RobV