Hi,
May this can help you:

 <?php

 // recusive tree function
 //
 function tree($id_parent = 0, $indent = 0)
    {
        global $g_tree, $ret_tree;
        $tt = array();
        $x = 0;
        $loop = 0;

        foreach($g_tree as $n)
        {         
            if( $n['id_parent'] == $id_parent)
            {
                $tt[$x++] = $loop;  
            }  
            $loop++;
        }
        /*-- if there are some parent ids --*/
        if( $x != 0){             
            foreach($tt as $d)
            {
                $tmp = array();
                
                foreach($g_tree[$d] as $key => $value)
                {                
                    $tmp[$key] = $value; 
                }
                $tmp['indent'] = $indent;

                $ret_tree[] = $tmp;
                
                tree($tmp['id'], $indent+1);
            }
        }
        else
        {
            return 0;
        }
    }


 // This is your tree structure. It should be sorted by titles
 //
 $g_tree = array();
 
 $g_tree[]  = array(
   'id' => 1,
   'id_parent' => 0,
   'title' => 'Astronomy'
  );   
 $g_tree[]  = array(
   'id' => 2,
   'id_parent' => 1,
   'title' => 'Radio-Astronomy'
  ); 
  $g_tree[]  = array(
   'id' => 3,
   'id_parent' => 2,
   'title' => 'Radio-Observatories'
  );   
  $g_tree[]  = array(
   'id' => 4,
   'id_parent' => 0,
   'title' => 'Physics'
  );  
  $g_tree[]  = array(
   'id' => 5,
   'id_parent' => 4,
   'title' => 'Lasers'
  );
  $g_tree[]  = array(
   'id' => 6,
   'id_parent' => 4,
   'title' => 'Photonics'
  );
  
  
 // This the the return array with indents
 //
 $ret_tree = array();
 
 tree(0,0);
 
 //Print the tree structure with indents
 //
 foreach($ret_tree as $cat)
 {
  $indent = '';
  for($i=0;$i<$cat['indent'];$i++)
   $indent .= '___';
  
  echo $indent.$cat['title'].'<br>';
 }

?>

It sould even work to print a subtree eg. tree(1,0)

atur


_______________________________________________


"Mike Klemin" <[EMAIL PROTECTED]> wrote
> Hello,
> 
> If anyone have expamples of displaying tree structure.
> 
> Say I have array  $result[x][y] with folloowing data
> 
> X    Y["ID"]    Y["OWNER"]
> 0    1               0
> 1    2               1
> 2    3               2
> 3    4               2
> 4    5               3
> 5    6               2
> 6    7               2
> 
> 
> So, I want it displayed like
> 
> ID1
>     ID2
>         ID3
>             ID5
>         ID4
>         ID6
>         ID7
> 
> ========================
> 
> Please, help I tryed to do it with recursing but failed.
> 
> There is a test function which shows that php doesnt support recursing
> well:-
> 



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

Reply via email to