Hi Brian
I was almost, almost, almost there :) but I was missing a bit of the logic.

Thank you very much. You solve my present and future problems.

Greetings
pt2002


"Brian Kell" <[EMAIL PROTECTED]> escreveu na mensagem
news:[EMAIL PROTECTED]
> Well, first off, a little bit of formatting (line numbers added for
> clarity):
>
>   1 $tpl->assign("tree",
>   2     array(
>   3         "element" => array(
>   4             array(
>   5                 "name" => "test1",
>   6                 "element" => array(
>   7                     array(
>   8                         "name" => "test1.1"
>   9                     ),
> 10                     array(
> 11                         "name" => "test1.2",
> 12                         "element" => array(
> 13                             array(
> 14                                 "name" => "test1.2.1"
> 15                             ),
> 16                             array(
> 17                                 "name"  => "test1.2.2"
> 18                             )
> 19                         )
> 20                     )
> 21                 )
> 22             )
> 23         )
> 24     )
> 25 );
>
> It appears that each node is represented as an array. This array contains
> a "name" and, if the node has any children, another array called "element"
> that contains the child nodes. In addition, the root node is an element of
> some "superroot", which is the array starting on line 2. (So if you want
> to have multiple trees, you can; just define multiple nodes with a pid of
> 0.)
>
> I think the following functions should do approximately what you want. I'm
> assuming MySQL here, since that's what I'm used to. If you're not using
> MySQL, you should be able to adapt this to your database easily enough.
>
> function build_tree() {
>      $subtree = build_subtree(0);
>      if ($subtree !== NULL)
>          return array("element => $subtree);
>      else
>          return NULL;
> }
>
> function build_subtree($pid) {
>      $result = mysql_query("SELECT id, name FROM tbl WHERE pid='$pid'");
>      if (!$result)
>          die("Can't query for pid $pid: ".mysql_error());
>      if (mysql_num_rows($result)) {
>          $subtree = array();
>          while ($row = mysql_fetch_assoc($result)) {
>              $node = array();
>              $node['name'] = $row['name'];
>              $element = build_subtree($row['id']);
>              if ($element !== NULL)
>                  $node['element'] = $element;
>              array_push($subtree, $node);
>          }
>          return $subtree;
>      } else
>          return NULL;
> }
>
> Be careful with this, because I didn't actually test this at all. Run it
> on a few test cases and make sure it gives you what you want.
>
> Brian

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to