tree built recursive

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andreas kirschner

    #1

    tree built recursive

    Hello developers!

    I'm sticking in a problem.

    I want to read the folder hierarchy of the webserver and store the
    information in a tree, built like a linked list - you know 'a tree'.

    you see in function readsubtree() i'm storing the object child in the
    array of the actualfoldernod e. but this doesn't work out. it never
    arrives there.

    the tree is just built to one depth. although the recursion works as
    some echo outs show.

    please help

    cheers
    a-k

    let me post in the snippet:

    function readTree(){
    global $name_of_root;
    $this->rootNode = new FolderTreeNode( $this);
    $this->rootNode->name = $name_of_root;
    $this->rootNode->absPath = absolutePath("" );
    if (relativePath(g etcwd()) == "") {$this->rootNode->selected = true;}
    $this->readSubTree(&$ this->rootNode);
    }

    function readSubTree($ac tualFolderNode) {
    $actualAbsDir = $actualFolderNo de->absPath;
    $dir = @opendir($actua lAbsDir);
    if (!$dir) {return 0;}
    while ($entry = readdir($dir)){
    if (is_dir($actual AbsDir."/".$entry) && ($entry != ".." && $entry !=
    ".")){
    $child = new FolderTreeNode( $this);
    $child->name = $entry;
    $child->absPath = $actualAbsDir."/".$entry;
    $child->parentNode = $actualFolderNo de;
    if (getcwd() == $child->absPath) {$child->selected = true;}
    $actualFolderNo de->childNodes[] = $child;
    $this->readSubTree($c hild);
    }
    }
    }

    class FolderTreeNode{

    var $folderTree;

    var $name;
    var $absPath;
    var $parentNode;
    var $childNodes = array();
    var $selected;
    var $newElements;


    function FolderTreeNode( $folderTree){
    $this->folderTree = $folderTree;
    }
    }
  • Jason Dumler

    #2
    Re: tree built recursive

    andreas kirschner wrote:[color=blue]
    > Hello developers!
    >
    > I'm sticking in a problem.
    >
    > I want to read the folder hierarchy of the webserver and store the
    > information in a tree, built like a linked list - you know 'a tree'.
    >
    > you see in function readsubtree() i'm storing the object child in the
    > array of the actualfoldernod e. but this doesn't work out. it never
    > arrives there.
    >
    > the tree is just built to one depth. although the recursion works as
    > some echo outs show.
    >
    > please help
    >
    > cheers
    > a-k
    >
    > let me post in the snippet:
    >
    > function readTree(){[/color]
    <snip>
    Guess this is OK.[color=blue]
    > }
    >
    > function readSubTree($ac tualFolderNode) {[/color]
    <snip>
    This code looks OK too, except your function declaration needs to pass
    the $actualFolderNo de by reference instead of as a regular var.

    function readSubTree(&$a ctualFolderNode ) {

    unless you're using PHP 5.0, which is supposed to do this automatically.

    If you don't pass by reference, $actualFolderNo de is a copy of the
    object you're passing into the function, not the actual object itself.
    [color=blue]
    > }
    >[/color]

    Jason

    Comment

    • andreas kirschner

      #3
      Re: tree built recursive

      I solved the problem:

      the instantiation is now made as a reference as well as the storing in
      the childnodes array

      function readSubTree(&$a ctualFolderNode ){
      $actualAbsDir = $actualFolderNo de->absPath;
      $dir = @opendir($actua lAbsDir);
      if (!$dir) {return 0;}
      while ($entry = readdir($dir)){
      if (is_dir($actual AbsDir."/".$entry) && ($entry != ".." && $entry
      != ".")){
      $child = &new FolderTreeNode( $this);
      $child->name = $entry;
      $child->absPath = $actualAbsDir."/".$entry;
      $child->parentNode = $actualFolderNo de;
      if (getcwd() == $child->absPath) {$child->selected = true;}
      $actualFolderNo de->childNodes[] = &$child;
      $this->readSubTree($c hild);
      }
      if (is_file($actua lAbsDir."/".$entry)){
      if (strtotime("-".$this->wExplorer->coloredDays. " days") <
      (filemtime($act ualAbsDir."/".$entry))) {
      $actualFolderNo de->newElements = true;
      }
      }
      }
      closedir($dir);
      }

      Comment

      Working...