views:

161

answers:

1

Hi, We have nested folders with parent-child relationship. We use MySQL MyISAM DB. The data is stored in the DB in the following manner.

Every time a child folder is created in the nested structure, the previous parentID is added. I want to get the RootFolderID of a folder which is added in the hierarchy as tabulated below.

FoldID  ParentID |RootFolderID
-----------------|-------------------
1         0      | 0
2         1      | 1
3         2      | 1
4         3      | 1
5         4      | 1

Please let me know how to get the root folderID and populate it in the RootFolderID column after a folder is created each time.

Thanks.

+2  A: 

if you know the parent folder's information, then the root folder will be the same as that of the parent. or, if the parent has no root folder, then the root folder will be the parent folder itself.

oedo
We have 100's of main folders and sub folders. When a sub folder is created, we need to populate the RootFolderID of the newly created folder.
Vish
yes, i understand that. since you know what the parent folder of the newly created folder is, then you can retrieve the root folder of the parent folder. if it's 0, then the root folder of the newly created folder is the parent folder id. if it's not 0, then the root folder of the newly created folder is the same as the root folder of the parent folder.
oedo
Yes, want to know how to retrieve the root folder of the parent folder of a newly created folder. It has to loop all the way to the root and then fetch the id. Pl let me know the query.
Vish
why does it have to loop all the way to the root? surely the root folder of a newly created folder will be the same as the root folder of the parent folder it's created in?
oedo
Well, turns out that I am as dumb as the question suggests. I was into a lot of things, thanks, will check it out.
Vish
no worries mate, sometimes you just need a fresh pair of eyes on a problem :)
oedo