Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do Traverse Hierarchy? #47

Closed
idleup opened this issue Mar 4, 2014 · 3 comments
Closed

How do Traverse Hierarchy? #47

idleup opened this issue Mar 4, 2014 · 3 comments

Comments

@idleup
Copy link

idleup commented Mar 4, 2014

Once you have a tree hierarchy using the ->toHiearchy() method how do you traverse it to output a

    list or options for a drop down list?

@ghost
Copy link

ghost commented Mar 6, 2014

you would want a recursive function, something like

public function printTree($root){
    $html = '';
    $html .= "<ul>";
    foreach($root as $r){

        $html .= "<li>". $r->name;
        if(count($r->children) > 0) {
            $html .= $this->printTree($r->children);
        }
        $html .= "</li>";
    }
    $html .= "</ul>";
    return $html;
}

this will produce a list with appropriate sub-lists.

for options for a drop down you could indent the hierarchy with something like:

public function printSelect($root){
    $html = '';
    $html .= "<select>";
    foreach($root as $r){
        $html .= $this->printOption($r);
    }
    $html .= "</select>";
    return $html;
}
public function printOption($node){
    $level = $node->getLevel();
    $indent = "";
    for($i = 0; $i < $level; $i++){
      $indent .= "&nbsp;|-&nbsp;";
    }
    $html = '<option value="'.$node->id . '">'.$indent . $node->name . '</option>';
    foreach($node->children as $child){
            $html .= $this->printOption($child);
    }
    return $html;
}

@idleup
Copy link
Author

idleup commented Mar 6, 2014

Perfect thanks, that is exactly what I needed.

@idleup idleup closed this as completed Mar 6, 2014
@kampit
Copy link

kampit commented Mar 13, 2014

@idleup how about with a hyperlink to the printTree?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants