adapted to your database ... for errors and more information .. let me know



<?

# Simple function 2 create an indentation according to the level/depth of
the tree node

function PrintNode ( $level , $text) { # função para imprimir um nó e fazer
o indentamento
        if ( $level == 0 ) {
                                return "$text<br>";
                }

        echo "<img src=\"images/spacer.jpg\" width=\"". 5 * $level . "%\"
height=\"20\">$text<br>";
}

# Main function, to be called recursively

function Parse_Tree ( $level=0, $parent=0 ) {

                $db = mysql_connect("localhost", "root", "password");
                mysql_select_db("mydb",$db);

                $sql = "SELECT * FROM table WHERE parent_id=$parent" ;
                $result = mysql_query($sql,$db);

                if ($result == false) die("failed");

                if ( mysql_num_rows($result) == 0 ) return 0 ;

                while ($fields = mysql_fetch_row($result)) {

                        Print_Node( $level, "<". $fields[0] . " >" . "<". $fields[1] . 
">" . "<".
$fields[2] . ">");

                        Parse_Tree ( $level + 1 , $fields[0] ) ; # Check if the 
current node as
childs and so on, recursively

                }

                return 0;
}
?>






-----Mensagem original-----
De: Smita Manohar [mailto:[EMAIL PROTECTED]]
Enviada: terça-feira, 27 de Agosto de 2002 9:33
Para: [EMAIL PROTECTED]
Assunto: [PHP-DB] how to display tree structure from database???


hiii
im using php with mysql. i have one table for which i want to display tree
structure.
for eg. table is like this...

person_id          name           parent_id

1                 shyam           0
2                 rohit           1
3                 nikita          1
4                 ram             0
5                 ankit           4
6                 jui             5
7                 vicky           5
8                 rekha           0
9                 jay             6
10                 kirti           8

i want help for writing query to display all topnodes, parent, their
childern and grand children

thnx and regards,
smita



_________________________________________________________________
MSN Photos is the easiest way to share and print your photos:
http://photos.msn.com/support/worldwide.aspx


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



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

Reply via email to