0
+-------+------+-----------+------------+
|user_id| name | parent_id |grandparentID
+----+--------+-----------+-------------+
|  1 | one    |      NULL |     NULL   |
|  2 | two    |         1 |     NULL   |
|  3 | three  |         1 |      1     |
|  4 | four   |         2 |      1     |
|  5 | five   |         2 |      2     |
|  6 | six    |         2 |      2     |
|  7 | seven  |         2 |      2     |
|  8 | eight  |         3 |      3     |
|  9 | nine   |         3 |      2     |
| 10 | ten    |         3 |      1     |
| 11 | eleven |         3 |      2     |
+-------------+-----------+------------+

I want to create a tree structure with the following DB schema Something like this below

                                 One
                                  |
       two______________three______________four______________five______________Six
       |                |                  |                  |                 |
    n1 n2 n3 n4 n5   t1 t2 t3 t4 t5   h1 h2 h3 h4 h4    a1 a2 a3 a4 a5   s1 s2 s3 s4 s5

I wrote the following code

<?php   
    function display_tree() {  
        $result = $this->qry("SELECT * FROM users WHERE Parent='?' or Parent ='?';" ,
        'in(select id from user_id from users where Parent = "'.$_SESSION['id'].'")', $_SESSION['id']); 

        echo '<div align="center">'.$_SESSION['name'].'<br></div>';
        echo '<div align="center">|</div>';
        echo '<div align="center">';
        while( $row=mysql_fetch_assoc($result)){
            echo "___".$row['name'].$row['user_id'].'';
        };
        $result1 = $this->qry("SELECT * FROM users where grandParent='".$_SESSION['id']."'");
        echo '<br>|<br>';
        while( $row1 =mysql_fetch_assoc($result1)){ 
            echo "--".$row1['name'];  
        }
        echo '</div>';
    }
?>

This is what it displays

             one
               |
    ___two___three
               |
  --four--five--six--seven--eight--

But I wish to have it in the order up above, not this, I have read tutorials but they all dwell on right left branching, none seems to touch on my type

Any links to good examples, I'll be glad

Any insight on my problem would also help me alot

I seem to have reached a fix end for me Thanks in advance.

1
  • 3
    Your code indention is awfully... wobbly. Do yourself a big favor and try to keep it consistently indented. It's a chore to get in the habit of doing it consistently, but you'll thank yourself in the future. Jun 9, 2012 at 23:08

1 Answer 1

0

I would recommend using some kind of plugin like jquery plugin that would build the tree for you here are couple of plugins.

http://jquery.bassistance.de/treeview/demo/

http://www.jqwidgets.com/jquery-widgets-demo/#demos/jqxtree/treebindingtojson.htm

https://github.com/pioz/jquery-tree

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.