Avatar billede sitedev Nybegynder
28. august 2003 - 02:22 Der er 12 kommentarer og
1 løsning

genbruge værdier fra allerede udtrukket recordset

Har brug for at kunne loope ned igennem
recordsettet
for at finde alle records som hører til toppen af en struktur

ID    parentmenu     
1      1
2      1
3      1
4      1
5      2
6      2
7      2
8      2
9      5
10    5
12    9
13    9
14    10
15    10
16    10
17    10
18    14
19    14
20    14

eks.

slette ID=10

ID 10 har nogle childs som hedder 14,15,16 & 17

disse har så nogle childs, f.eks 14 har childs 18,19 & 20

jeg har brug for at kunne slette alle disse ned i uendeligt mange sublevels.
indtil videre kan jeg kun kode så root og 1 child dvs. 10+14+15+16+17 bliver slettet.
hvor det rent faktisk er 10+14+15+16+17+18+19+20 og endnu flere hvis der var flere sublevels der skulle slettes.

Kan ikke lige lure hvordan jeg looper igen og igen og igen (uendeligt eller til sidste child) indtil alle der hører til den root er slettet. ;-)

pseudokode
<--
do while not rs.eof
delete rs("id")

    do while not rs2.eof
    delete rs2("id")
    rs2.movenext
    loop

rs.movenext
loop
-->

kan nogle hjælpe ????

med venlig hilsen
Bo Petersen
Avatar billede terry Ekspert
28. august 2003 - 12:15 #1
You need to make a function which calls itself (recursive).

The first call to the function ALL records are read then for each record in the loop you call the same function with one paramater (the parent ID) which again calls the function. So your looping down the root structure visiting each Parent ID. Then if you find there are NO children you are obviosuly at the bottom so you can delete the record.
In other words you delete from the bottom and UP.
Avatar billede terry Ekspert
28. august 2003 - 12:16 #2
I notice something with your table!

If a record has no parent then the ID should be 0 NOT 1
Avatar billede sitedev Nybegynder
28. august 2003 - 13:25 #3
yes i know ;-), the table was just for reference. i the original database the rootlevels have a parent of -1

thanks anyway
Avatar billede terry Ekspert
30. august 2003 - 11:53 #4
do you have a solution now?
Avatar billede terry Ekspert
30. august 2003 - 12:05 #5
just another comment>Yiuyr only allowed to have one question on the same problem! I suggest you close one of them!
Avatar billede sitedev Nybegynder
30. august 2003 - 13:48 #6
Sorry about that, did not know that, how do i delete a question anyway.

anyway, i have come up with this solution, but it does not work properly

<!--#INCLUDE FILE="dbconn.asp" -->

<%
nRootID = request("id")
response.write "<br>before function " & nRootID
' response.write nRootID

    set rs=conn.execute("SELECT * FROM menutree")

FUNCTION deleteRoot(nRootID)

    set rs=conn.execute("SELECT * FROM menutree WHERE parentid =  "& nRootID )

    do while not rs.EOF
'        deleteRoot rs("id")
response.write "<br>within function " & rs("id") & nRootID
        rs.MoveNext
    loop

delete =    ("DELETE FROM menutree WHERE id = " & GetID)
' conn.execute(delete)
response.write delete

END FUNCTION
response.write "<br>after function " & nRootID
deleteRoot nRootID
%>

very messy code i know, but i've kept my changes in for reference.

kan nogen hjælpe....;-)
Avatar billede terry Ekspert
30. august 2003 - 15:23 #7
to close a question you just place an answer yourself an accept it, or accept one of the other answers!
Avatar billede terry Ekspert
30. august 2003 - 15:23 #8
I wasnt aware that your solution was for ASP!
Avatar billede terry Ekspert
31. august 2003 - 09:19 #9
thanks, hope you find a solution which works
Avatar billede sitedev Nybegynder
31. august 2003 - 12:24 #10
I did, the code above is correct, except that this line
set rs=conn.execute("SELECT * FROM menutree WHERE parentid =  "& nRootID )

should read
set rs=conn.execute("SELECT * FROM menutree WHERE parentmenu =  "& nRootID )

because of the database column name
instead. thanks anyway..., ;-) have a nice weekend....
Avatar billede terry Ekspert
31. august 2003 - 12:27 #11
OK, I have also posted an example in your other question which actually looks very similar to yours :o)
Avatar billede sitedev Nybegynder
31. august 2003 - 15:52 #12
anyway it works, and i have accepted your answer
Avatar billede terry Ekspert
31. august 2003 - 16:00 #13
great, og tak!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester