Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    help me to produce proper output.

    Hi,
    this could be really simple for producing the output in html format by using the <ul> <li>

    this is a tree traversal function and is working fine. But i want to display the output in proper <ul> and <li> format and im not able to put <ul> and <li> at proper place. please anyone help me to put these at proper place

    here is the code

    PHP Code:
        <?php

    include("includes/connection.php");

    traversal("user_1");
    ?>

    <?php
    $cnt_left 
    = -1;
    $cnt_right = -1;
    $l ;
    function 
    traversal($root)
    {
    global 
    $cnt_left;
    global 
    $cnt_right;
    global 
    $l;
    $sql "SELECT * FROM users where root_id= '$root' ";
    $res mysql_query($sql);
    $row mysql_fetch_array($res);

    if(
    $root!="")
    {

          if(
    $l == "L")
          {
              echo 
    "<ul><li>";
          }

          if(
    $l == "R")
          {

              
    //echo $root . " : $l ";
              
    echo "</li>";
              echo 
    "</ul>";
              echo 
    "</li>";
              echo 
    "<li>";            
    //            echo "</li>";
          
    }

    //        echo "Root = ".$root;

          
    echo $root " : $l ";
      if(
    $row['left_flag'] > 0)
      {
          
    $cnt_left $cnt_left +1;
          
    $l ="L";


          
    traversal($row['left_node']);

      }
      else
      {

           
    $cnt_left $cnt_left +1;
                  
    $l ="R";

      }
      if(
    $row['right_flag'] > 0)
      {
          
    $cnt_right $cnt_right +1;


          
    traversal($row['right_node']);
      } 
      else
      {

           
    $cnt_right $cnt_right +1;
      }

    else
    {
      echo 
    "No Root";
    }

    }

    ?>
    Last edited by Inigoesdr; 09-18-2011 at 06:49 PM.

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    I'm struggling to follow your tree structure. It looks kind of like a double-linked list, but I'm unsure. Can you describe your tree a bit more?

    Any tree structure I've implemented has always carried an 'offset' parameter in the recursion - which holds a value for depth. Regardless, <ul><li> should be easy to implement. A new tree is started with <ul> with it's first node as <li>, so your recursion would check for a new node at this depth using whatever identifier you're using, if exists - echo/return <ul><li> current node + recursion </li></ul>; else - return empty string.

  • #3
    New Coder
    Join Date
    Nov 2010
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BluePanther View Post
    I'm struggling to follow your tree structure. It looks kind of like a double-linked list, but I'm unsure. Can you describe your tree a bit more?

    Any tree structure I've implemented has always carried an 'offset' parameter in the recursion - which holds a value for depth. Regardless, <ul><li> should be easy to implement. A new tree is started with <ul> with it's first node as <li>, so your recursion would check for a new node at this depth using whatever identifier you're using, if exists - echo/return <ul><li> current node + recursion </li></ul>; else - return empty string.
    Hi BluePanther,
    thank you very much for you reply, i have spent ages on searching on the internet for the solution but could not found the solution, thats y i created this thread.

    And thank you for your interest in my problem.
    my code displays the tree like the image below and that is incorrect display of this tree :-(





    Here im writing all the info im using for generating the tree.

    This is simple binary tree.

    in the database table there is a field
    my_node_id ( this is just the node ID of the current user )
    root_id ( this field contain the id of its parent)
    left_node ( this field contain the ID of the node that is assigned on the left side of the parent / node / this row )
    right_node ( this field contain the ID of the node that is assigned on the right side of the parent / node / this row )

    table

    Code:
    CREATE TABLE IF NOT EXISTS `tbl_users_temp` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `password` varchar(255) NOT NULL,
      `my_node_id` varchar(255) NOT NULL,
      `left_flag` int(11) NOT NULL,
      `right_flag` int(11) NOT NULL,
      `root_id` varchar(255) NOT NULL,
      `left_node` varchar(255) NOT NULL,
      `right_node` varchar(255) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=41 ;
    
    --
    -- Dumping data for table `tbl_users_temp`
    --
    
    INSERT INTO `tbl_users_temp` (`id`, `name`, `password`, `my_node_id`, `left_flag`, `right_flag`, `root_id`, `left_node`, `right_node`) VALUES
    (1, 'A', '1234', 'myuser00_1', 1, 1, '', 'myuser00_2', 'myuser00_3'),
    (2, '2', '1234', 'myuser00_2', 1, 1, 'myuser00_1', 'myuser00_4', 'myuser00_6'),
    (3, '3', '1234', 'myuser00_3', 1, 1, 'myuser00_1', 'myuser00_5', 'myuser00_7'),
    (4, '4', '1234', 'myuser00_4', 1, 1, 'myuser00_2', 'myuser00_8', 'myuser00_12'),
    (5, '5', '1234', 'myuser00_5', 1, 1, 'myuser00_3', 'myuser00_10', 'myuser00_14'),
    (6, '6', '1234', 'myuser00_6', 1, 1, 'myuser00_2', 'myuser00_9', 'myuser00_13'),
    (7, '7', '1234', 'myuser00_7', 1, 1, 'myuser00_3', 'myuser00_11', 'myuser00_15'),
    (8, '8', '1234', 'myuser00_8', 1, 1, 'myuser00_4', 'myuser00_16', 'myuser00_24'),
    (9, '9', '1234', 'myuser00_9', 1, 1, 'myuser00_6', 'myuser00_18', 'myuser00_26'),
    (10, '10', '1234', 'myuser00_10', 1, 1, 'myuser00_5', 'myuser00_20', 'myuser00_28'),
    (11, '11', '1234', 'myuser00_11', 1, 1, 'myuser00_7', 'myuser00_22', 'myuser00_30'),
    (12, '12', '1234', 'myuser00_12', 1, 1, 'myuser00_4', 'myuser00_17', 'myuser00_25'),
    (13, '13', '1234', 'myuser00_13', 1, 1, 'myuser00_6', 'myuser00_19', 'myuser00_27'),
    (14, '14', '1234', 'myuser00_14', 1, 1, 'myuser00_5', 'myuser00_21', 'myuser00_29'),
    (15, '15', '1234', 'myuser00_15', 1, 1, 'myuser00_7', 'myuser00_23', 'myuser00_31'),
    (16, '16', '1234', 'myuser00_16', 1, 0, 'myuser00_8', 'myuser00_32', '0'),
    (17, '17', '1234', 'myuser00_17', 1, 0, 'myuser00_12', 'myuser00_34', '0'),
    (18, '18', '1234', 'myuser00_18', 1, 0, 'myuser00_9', 'myuser00_36', '0'),
    (19, '19', '1234', 'myuser00_19', 0, 0, 'myuser00_13', '0', '0'),
    (20, '20', '1234', 'myuser00_20', 1, 0, 'myuser00_10', 'myuser00_38', '0'),
    (21, '21', '1234', 'myuser00_21', 0, 0, 'myuser00_14', '0', '0'),
    (22, '22', '1234', 'myuser00_22', 0, 0, 'myuser00_11', '0', '0'),
    (23, '23', '1234', 'myuser00_23', 0, 0, 'myuser00_15', '0', '0'),
    (24, '24', '1234', 'myuser00_24', 1, 0, 'myuser00_8', 'myuser00_33', '0'),
    (25, '25', '1234', 'myuser00_25', 1, 0, 'myuser00_12', 'myuser00_35', '0'),
    (26, '26', '1234', 'myuser00_26', 1, 0, 'myuser00_9', 'myuser00_37', '0'),
    (27, '27', '1234', 'myuser00_27', 0, 0, 'myuser00_13', '0', '0'),
    (28, '28', '1234', 'myuser00_28', 1, 0, 'myuser00_10', 'myuser00_39', '0'),
    (29, '29', '1234', 'myuser00_29', 0, 0, 'myuser00_14', '0', '0'),
    (30, '30', '1234', 'myuser00_30', 0, 0, 'myuser00_11', '0', '0'),
    (31, '31', '1234', 'myuser00_31', 0, 0, 'myuser00_15', '0', '0'),
    (32, '32', '1234', 'myuser00_32', 0, 0, 'myuser00_16', '0', '0'),
    (33, '33', '1234', 'myuser00_33', 0, 0, 'myuser00_24', '0', '0'),
    (34, '34', '1234', 'myuser00_34', 0, 0, 'myuser00_17', '0', '0'),
    (35, '35', '1234', 'myuser00_35', 0, 0, 'myuser00_25', '0', '0'),
    (36, '36', '1234', 'myuser00_36', 1, 0, 'myuser00_18', 'myuser00_40', '0'),
    (37, '37', '1234', 'myuser00_37', 0, 0, 'myuser00_26', '0', '0'),
    (38, '38', '1234', 'myuser00_38', 0, 0, 'myuser00_20', '0', '0'),
    (39, '39', '1234', 'myuser00_39', 0, 0, 'myuser00_28', '0', '0'),
    (40, '40', '1234', 'myuser00_40', 0, 0, 'myuser00_36', '0', '0');
    and here is the code

    PHP Code:
    <?php
    error_reporting
    (5);
    include(
    "includes/connection.php");
        echo 
    "<div id=\"TreeContainer\"><ul id=\"tree\"><li> ME";
    traversal("myuser00_1");
     echo 
    "</li></ul></div>";
     
    ?>
        
    <?php
    $cnt_left 
    = -1;
    $cnt_right = -1;
    $l ;
    $i;
    function 
    traversal($root)
    {
    global 
    $cnt_left;
    global 
    $cnt_right;
    global 
    $l;
    global 
    $i;
    $sql "SELECT * FROM tbl_users_temp where my_node_id= '$root' ";
    $res mysql_query($sql);
    $row mysql_fetch_array($res);
     if(
    $root!="")
    {
              if(
    $l == "L")
            {
                print 
    "<ul>\n";
                    print 
    "<li>\n";
                        print 
    "$row[name] $flag ";
                    print 
    "</li>\n";
     
             }
            
            if(
    $l == "R")
            {
                      print 
    "<li>\n";
                        print 
    "$row[name] ";
                    print 
    "</li>\n";
                                 
             }
         if(
    $row['left_flag'] > 0)
        {
            
    $cnt_left $cnt_left +1;
            
    $l "L";
               
    traversal($row['left_node']);
            
         }
        else
        {
              
    $cnt_left $cnt_left +1;
           }
        if(
    $row['right_flag'] > 0)
        {
            
    $cnt_right $cnt_right +1;
            
    $l "R";
             
    traversal($row['right_node']);
             
        } 
        else
        {
              
    $cnt_right $cnt_right +1;
             print 
    "</ul>\n";
         }
     }    
    else
    {
        echo 
    "No Root";
    }

    }

    ?>
    this
    Attached Thumbnails Attached Thumbnails help me to produce proper output.-temp.jpg  
    Last edited by chunk; 09-18-2011 at 07:10 PM.

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    When you say incorrect display of the tree, have you checked the actual output order to make sure the tree is being outputted properly? Have you confirmed that you're getting closing tags all the way through, in the right order?

  • #5
    New Coder
    Join Date
    Nov 2010
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BluePanther View Post
    When you say incorrect display of the tree, have you checked the actual output order to make sure the tree is being outputted properly? Have you confirmed that you're getting closing tags all the way through, in the right order?
    hi BluePanther,
    im getting all the nodes properly with the recursion, But im not able to produce the correct tree, Because im not able to put the opening and closing tags of UL and LI at proper place :-(
    thats y i posted the code if you could find the proper place to put them :-|

    thank you

  • #6
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Ok. It's just when you post a screenshot and say 'this is wrong' without explanation, it leads me to validate what is actually wrong.

    Show me two pages, both source and output, one with the recursion the way it was before, and one with your attempt at the list tags so I can see the difference.

  • #7
    New Coder
    Join Date
    Nov 2010
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BluePanther View Post
    Ok. It's just when you post a screenshot and say 'this is wrong' without explanation, it leads me to validate what is actually wrong.

    Show me two pages, both source and output, one with the recursion the way it was before, and one with your attempt at the list tags so I can see the difference.
    Hi BluePanther,
    i have already given the source on #3 with database, this is just a binary tree and nothing else :-)
    the correct tree should like the image below :-)

    Attached Thumbnails Attached Thumbnails help me to produce proper output.-tr.jpg  

  • #8
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    If you notice, I actually asked to see the code that displays the correct tree, but in the format you don't want - the code that produces the screen capture you provided in that reply.

    Also, any particular reason why you're using print instead of echo? Not really important, just curious.

  • #9
    New Coder
    Join Date
    Nov 2010
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BluePanther View Post
    If you notice, I actually asked to see the code that displays the correct tree, but in the format you don't want - the code that produces the screen capture you provided in that reply.

    Also, any particular reason why you're using print instead of echo? Not really important, just curious.
    hi BluePanter,
    i also used this code on the URL below. And i integrated it with my database. The database i have already pasted in #3
    Code:
    http://snipplr.com/view/22864
    Actually i can't understand the code written in this URL, that'y i can't modify this code.

    and also this code need some modifications to display the desired user's tree in the database.

    This code only displays the correct tree for first user in the database with this query
    Code:
    SELECT * FROM table
    And the correct tree is produced by this code. But this only displays the tree of first user in the database. if i change the query to


    Code:
    SELECT * FROM table where id > 3
    to view the tree of user whose id is 4.

    then this code go to an infinite loop.

    But at last i only want a code that produce the tree with my table structure. And i can't get anything useful :-(

    If you are going to try this code, provided by above URL. Then these fields in database

    left_node
    right_node
    root_id

    must remove myusers00_ from each row. Only number after _ should be the value on each row for these three columns to work with this code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •