You can do it only in few lines of code with a recursive function... You
can display full tree structure with unlimited  levels.

Mysql Table  :
cid int,
parent int,
Name varchar

Cid     parent  varchar
1       0       A
2       1       C
3       0       B
4       1       D
5       2       E
6       3       F
This should output :

- A
- - C
- - - E
- - D
- B
- - F


  function ShowTree($parent=0,$level=0){
    $query=mysql_query("select cid,parent,name from categories where
parent='$parent'");
    $level++;
    while ($row=mysql_fetch_object($query) ){
       $cnt=$row->cid;
       $element[$cnt]['cid']=$row->cid;
       $element[$cnt]['parent']=$row->parent;
       $element[$cnt]['name']=$row->name;
       echo str_repeat('-', $level) ."<B>".$element[$cnt]['name']."</B>
(level=$level)(counter=$cnt)<br>";
       ShowTree($element[$cnt]['cid'],$level);
     }
  }

 ShowTree(0,0);

> -----Original Message-----
> From: MindHunter [mailto:[EMAIL PROTECTED]] 
> Sent: Wednesday, November 28, 2001 9:27 AM
> To: [EMAIL PROTECTED]
> Subject: [PHP] Hierarchies
> 
> 
> Hi,
> 
> I need to display hierarchies (hierarchical structures).  I 
> have a MYSQL table with data like this:
> 
> Node,Parent
> A,
> B,A
> C,A
> D,B
> E,B
> F,D
> G,A
> 
> It can go down to 10 levels.
> 
> I do not want display one level and then click to display the 
> next, I want to see the entire tree at once, say like this:
> 
> A
> ---B
> ------D
> ---------F
> ------E
> ---C
> ---G
> 
> Any ideas, scripts on how I can do this?  I seems like I have 
> to use a recursive function and /or arrays?
> 
> Any help will be appreciated!
> 
> Thanks
> MH
> 
> 
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: 
> [EMAIL PROTECTED] To contact the list 
> administrators, e-mail: [EMAIL PROTECTED]
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to