Skip to content Skip to sidebar Skip to footer

How To Show Tree View In Php?

I am developing Tree View in php, how to connect my php array code in html UI designs. My Php code is: 100, 'pare

Solution 1:

We can use a "real" tree in php code and that makes the things much easier.

classBranch{
  public$branches = [];
  public$name;

  publicfunction__construct($name) {
    $this->name = $name;
  }

  publicfunctionto_html() {
    $str = '<li><a href="#">' . $this->name . "</a>\n";
    if (!empty($this->branches)) {
      $str .= '<ul>';
      foreach ($this->branches as$branch) {
        $str .= $branch->to_html();
      }
      $str .= '</ul>';
    }
    $str .= "</li>\n";
    return$str;
  }
}

$tree = new Branch("first");
$tree->branches[] = new Branch("second");
$tree->branches[] = new Branch("third");
$tree->branches[] = new Branch("fourth");
$tree->branches[0]->branches[] = new Branch("five");
$tree->branches[0]->branches[] = new Branch("eight");
$tree->branches[0]->branches[] = new Branch("nine");
$tree->branches[1]->branches[] = new Branch("six");
$tree->branches[1]->branches[] = new Branch("ten");
$tree->branches[2]->branches[] = new Branch("seven");

$html = $tree->to_html();

The output html will be

  • first
    • second
      • five
      • eight
      • nine
    • third
      • six
      • ten
    • fourth
      • seven
  • Solution 2:

    as php is html embedeble code you can use php any where in html like after head before head after body and so on.. just echo what you want user to see in html code for example

    Post a Comment for "How To Show Tree View In Php?"