N-Level Tree PHP MYSQL HELP
문제
I have been looking at a N-level tree class.
Could anyone possibly advise on the best way I could display the results ie, the looping of the result set, should I use DIVs or ULs?
An example result set will look like so if you find the article too long to read
Array ( [531] => stdClass Object ( [id] => 531 [rid] => 0 [created] => 1255384549 [nleft] => 1 [nright] => 8 [nlevel] => 1 ) [534] => stdClass Object ( [id] => 534 [rid] => 531 [created] => 1255384549 [nleft] => 2 [nright] => 3 [nlevel] => 2 ) [532] => stdClass Object ( [id] => 532 [rid] => 531 [created] => 1255384587 [nleft] => 4 [nright] => 7 [nlevel] => 2 ) [533] => stdClass Object ( [id] => 533 [rid] => 532 [created] => 1255384628 [nleft] => 5 [nright] => 6 [nlevel] => 3 ) )
Any advice is apreciated
Thanks
해결책
Found the solution I was looking for.
function _generateTreeData(&$arr, $id, $level, &$n)
{
$arr[$id]->nlevel = $level;
$arr[$id]->nleft = $n++;
// loop over the node's children and process their data
// before assigning the nright value
foreach ($arr[$id]->children as $child_id) {
$this->_generateTreeData($arr, $child_id, $level + 1, $n);
}
$arr[$id]->nright = $n++;
}
Now onto the formatting
다른 팁
Kinda depends on what you would need to do with the output. I would personally use unsorted lists or definition lists.
What are you trying to do?
Output it as ULs and LIs. If the output is too complex, you can put a JavaScript "Tree view" component on top of it - preferably one that can turn a UL into a tree view automatically.
제휴하지 않습니다 StackOverflow