Recursive Parent / Child
Posted: Thu Sep 03, 2009 3:58 am
Hi,
The topic title may not be very clear but hopefully i can explain it better.
I have a table of pages, and a field for Parent ID, If a page has no parent, the Parent ID is 0, and obviously if a Page has a Parent, the Parent ID is the id of that page.
Example Data
ID: 1, Page: Home, Parent ID: 0, url: home
ID: 2, Page: Sub-page, Parent ID: 1, url: sub-page
Now the situation i have is, I select a page (Sub-Page), which has an ID of 2, and what i want to do is have a function that can generate a url for this.
So if i gave this function an ID of 2, it will return home/sub-page, so if will recursively go through all the Parent ID's, retaining their url until there are no more parents and then return the url.
So if Sub-page(2) had a further child page called Sub-Sub-Page (ID 3), then it will return home/sub-page/sub-sub-page/
I'm not sure on how to approach this, and hoping someone can help me out.
Thanks.
The topic title may not be very clear but hopefully i can explain it better.
I have a table of pages, and a field for Parent ID, If a page has no parent, the Parent ID is 0, and obviously if a Page has a Parent, the Parent ID is the id of that page.
Example Data
ID: 1, Page: Home, Parent ID: 0, url: home
ID: 2, Page: Sub-page, Parent ID: 1, url: sub-page
Now the situation i have is, I select a page (Sub-Page), which has an ID of 2, and what i want to do is have a function that can generate a url for this.
So if i gave this function an ID of 2, it will return home/sub-page, so if will recursively go through all the Parent ID's, retaining their url until there are no more parents and then return the url.
So if Sub-page(2) had a further child page called Sub-Sub-Page (ID 3), then it will return home/sub-page/sub-sub-page/
I'm not sure on how to approach this, and hoping someone can help me out.
Thanks.