views:

113

answers:

2

i have this recursive code

<ul>
<%
sql="SELECT * FROM cats ORDER BY orderid " 
rs.Open sql,Conn
dim recArray
If Not rs.EOF Then
    recArray = rs.getRows()
    dim i
    for i=0 to uBound(recArray,2)
        if recArray(1,i)=0 then 
            call showMessage(i) 
        end if
    next

End If

function showMessage(index)
    %><li><%=recArray(2,index)%></li><%
    for a=0 to uBound(recArray,2)
        if recArray(1,a) = recArray(0,index) Then 
            %><ul><%
            call showMessage(a)
            %></ul><%
        end if
    next
    %></li><%
end function
%>
</ul>

inside the loop in the function i have the for the sub(s) but after each line of li it will close the ul how can i have that dynamic and to have the output like this

<ul>
  <li></li>
  <li></li>
  <li>
    <ul>
      <li></li>
      <li></li>
      <li></li>
    </ul>
  </li>
  <li></li>
</ul>

and not like this

<ul>
  <li></li>
  <li></li>
  <li>
    <ul><li></li></ul>
    <ul><li></li></ul>
    <ul><li></li></ul>
  </li>
  <li></li>
</ul>
+1  A: 

Edited code:

<ul>
<%
sql="SELECT * FROM cats ORDER BY orderid " 
rs.Open sql,Conn
dim recArray
If Not rs.EOF Then
    recArray = rs.getRows()
    dim i
    for i=0 to uBound(recArray,2)
        if recArray(1,i)=-1 then 
            call showMessage(i) 
        end if
    next

End If

function showMessage(index)
    %><li><%=recArray(2,index)%><%
    subs = false
    for a=0 to uBound(recArray,2)
        if recArray(1,a) = recArray(0,index) Then 
            subs = true
        end if
    next
    if subs then
        %><ul><%
        for a=0 to uBound(recArray,2)
            if recArray(1,a) = recArray(0,index) Then 
                call showMessage(a)
            end if
        next
        %></ul><%
    end if
    %></li><%
end function
%>
</ul>
tloflin
not good... all of the categories are on the same level
Y.G.J
Could you give me some sample data?
tloflin
i have 9 cats, in the db i have id, fid, name and orderid fid is the fatherid - if there is no father it gets (-1) and what i get with the code you gave me is <ul> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> ofcourse with text inside the li most likly because you changed it to getRows and then you run over it with the for loop so it makes only the li inside it. it is never passing the if because now there is nothing to compare it for the first time it is in
Y.G.J
Ah, I see; try the updated code.
tloflin
nothing... only the ul is there never passes the condition: recArray(1,a) = recArray(0,index) if i change it to recArray(1,a) = -1 it is passes but then i get error Error Type: Microsoft VBScript runtime (0x800A0007) Out of memory: 'createobject' in the for b=0 to uBound(recArray,2) line
Y.G.J
Hmm, did you change the root from 0 to -1? Because your code shouldn't work either... I will update my code.
tloflin
not working with -1can you give me a recurstion code for a table that will go over the records - and set them as UL list with childrens?
Y.G.J
OK, I went back to your original code, since that will be easier for you to fix. The idea is, you have to add another loop to find out whether to show the `<ul>` or not, because you need to know that before you are in the loop, not while you're in it.
tloflin
almost... now i get<ul><li>first</li><li>second</li><li><ul><li>first-sub</li><li>second-sub</li></ul></li></ul>the sub ul is under a new li and not under the second li
Y.G.J
Oh, thought that was what you wanted. That's easy to fix.
tloflin
thanks man - now i have other things to work around it - are you good with jquery and sortable function?
Y.G.J
Sorry, I'm not that great at jQuery myself. Somebody out there is, though; start a new question.
tloflin
A: 

hi there,

can someone please give me the working code for recursive loop to fetch n level category / subcategories in vbscript using getrows.

Y.G.J, do you have the working code for the cat / sub cat tree in UL / LI, can you please email it to me. I need it very badly.

regards hassan

Ali
the edited code above (by tloflin) is working for me
Y.G.J