In my humble experience I have found that it is better practice to link to the parent only. One seems to get into a lot less problems that way.
So what you do is have a function called getparent($child)
And then you great a functionlike this:
function getgrandparent($grandchild){
return getparent(getparent($grandchild)));
}
I guess it is even better to have a function that get's the nth parent, like so:
// Returns the nth parent of the parsed child
function getnthparent($child,$n){
$newchild=$child;
for($i=1;$i<=$n;$i++){
$newparent=getparent($newchild);
$newchild=$newparent;
}
return $newparent;
}
I think the above works - I haven't tested it - I may have a bug in the logic here and there. The aim is that to get the parent you would type:
getnthparent($child,1)
To get a grandparent you type:
getnthparent($child,2)
Hope that helps.
Andyman