Indentation Like binary tree

Discussion in 'PHP' started by Om ji Kesharwani, Jan 24, 2010.

  1. #1
    Hello friends,
    I am developing MLM site and using this php code for traversal
    <?
    $connection = mysql_connect("localhost", "root", "");
        $database = mysql_select_db("chek", $connection);
    ?>
       <?php  
    
    function display_tree($root) {  
     
       // retrieve the left and right value of the $root node  
       $result = mysql_query('SELECT lft, rgt FROM mptt '.  
                              'WHERE title="'.$root.'";');  
       $row = mysql_fetch_array($result);  
     
       // start with an empty $right stack  
       $right = array();  
     
       // now, retrieve all descendants of the $root node  
       $result = mysql_query('SELECT title, lft, rgt FROM mptt '.  
                              'WHERE lft BETWEEN '.$row['lft'].' AND '.  
                              $row['rgt'].' ORDER BY lft ASC;');  
     
       // display each row  
       while ($row = mysql_fetch_array($result)) {  
           // only check stack if there is one  
           if (count($right)>0) {  
               // check if we should remove a node from the stack  
               while ($right[count($right)-1]<$row['rgt']) {  
                   array_pop($right);  
               }  
           }  
           // display indented node title  
           echo "<span style=color:red;>".str_repeat('&nbsp;',count($right)).count($right).$row['title']."\n<span>";  
     
           // add this node to the stack  
           $right[] = $row['rgt']; 
    
       }
     
       
    }  
    display_tree(food);
    ?>
    
    PHP:
    The output is in this format:
    Food
    Fruit
    Red
    Cherry
    Strawberry
    Yellow
    Banana
    Meat
    Beef
    Pork

    I want to show it like:
    [​IMG]

    Please help me ....
    Thank u ...
     
    Om ji Kesharwani, Jan 24, 2010 IP