title: How to create binary tree in PHP using mysql database
body: <p>I want to print binary tree (Below Image) in php from database value <a href="https://i.stack.imgur.com/Y3QGB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y3QGB.png" alt="Expected output"></a>   </p>
  
  <p>And above is my database table structure <a href="https://i.stack.imgur.com/oJ0L4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oJ0L4.png" alt="Expected output"></a></p>
  
  <p>Here is what I tried, In this code I want to display binary tree of user id PX100 </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php 
  include 'db_connect.php';
  
  $user_id = 'PX100';
  $select_right_tree = $con-&gt;prepare("SELECT * FROM tbl_user_postion WHERE user_id=:user_id");
  $select_right_tree-&gt;bindParam(":user_id", $user_id);
  $select_right_tree-&gt;execute();
  $fetch_right_tree = $select_right_tree-&gt;fetch(PDO::FETCH_ASSOC);
  $ex_pos_right = $fetch_right_tree['pos_right'];
  $ex_pos_left = $fetch_right_tree['pos_left'];
   
  function right_part($pos_right) {
      if(!empty($pos_right)) {
          $select_left_tree = $con-&gt;prepare("SELECT pos_right, pos_left FROM tbl_user_postion WHERE user_id=:user_id");
          $select_left_tree-&gt;bindParam(":user_id", $pos_right);
          $select_left_tree-&gt;execute();
          $fetch_left_tree = $select_left_tree-&gt;fetch(PDO::FETCH_ASSOC);
          $user_right = $fetch_left_tree['pos_right'];
          $user_left = $fetch_left_tree['pos_left'];
          return array($user_right, $user_left);
          if(!empty($user_right)) {
              right_part($user_right);
          }
          if(!empty($user_left)) {
              left_part($user_left);
          }
      }
  }
  
  function left_part($pos_left) { 
      if(!empty($pos_left)) {
          $select_left_tree = $con-&gt;prepare("SELECT pos_right, pos_left FROM tbl_user_postion WHERE user_id=:user_id");
          $select_left_tree-&gt;bindParam(":user_id", $pos_left);
          $select_left_tree-&gt;execute();
          $fetch_left_tree = $select_left_tree-&gt;fetch(PDO::FETCH_ASSOC);
          $user_right = $fetch_left_tree['pos_right'];
          $user_left = $fetch_left_tree['pos_left'];
          return array($user_right, $user_left);
          if(!empty($user_left)) {
              left_part($user_left);
          }
          if(!empty($user_right)) {
              right_part($user_right);
          }
      }
  }
  ?&gt;
  
  &lt;div class="tree"&gt;
      &lt;ul&gt;
          &lt;li&gt;
              &lt;a href="#"&gt;&lt;img src="image/user.png" &gt;&lt;br/&gt;&lt;?php echo $user_id; ?&gt;&lt;/a&gt;
              &lt;ul&gt;
                  &lt;li&gt;
                      &lt;a href="#"&gt;&lt;img src="image/user.png"&gt;&lt;br/&gt;&lt;?php echo $ex_pos_right; ?&gt;&lt;/a&gt;
                      &lt;?php 
                      if(!empty($ex_pos_right))
                      { 
                          list($pos_right1, $pos_left1) = right_part($ex_pos_right);
                     ?&gt;
                              &lt;ul&gt;
                                  &lt;li&gt; &lt;a href="#"&gt;&lt;img src="image/user.png"&gt;&lt;br/&gt;&lt;?php echo $pos_right1; ?&gt;&lt;/a&gt;  &lt;/li&gt;
                                  &lt;li&gt; &lt;a href="#"&gt;&lt;img src="image/user.png"&gt;&lt;br/&gt;&lt;?php echo $pos_left1; ?&gt;&lt;/a&gt;  &lt;/li&gt;
                              &lt;/ul&gt;
                          &lt;?php 
                          }
                          ?&gt;
                      &lt;/li&gt;
                      &lt;li&gt;
                          &lt;a href="#"&gt;&lt;img src="image/user.png"&gt;&lt;br/&gt;&lt;?php echo $ex_pos_left; ?&gt;&lt;/a&gt; 
                          &lt;?php 
                          if(!empty($ex_pos_left))
                          { 
                              list($pos_right2, $pos_left2) = left_part($ex_pos_left);
                         ?&gt;
                              &lt;ul&gt;
                                  &lt;li&gt; &lt;a href="#"&gt;&lt;img src="image/user.png"&gt;&lt;br/&gt;&lt;?php echo $pos_right2; ?&gt;&lt;/a&gt;  &lt;/li&gt;
                                  &lt;li&gt;  &lt;a href="#"&gt;&lt;img src="image/user.png"&gt;&lt;br/&gt;&lt;?php echo $pos_left2; ?&gt;&lt;/a&gt;  &lt;/li&gt;
                              &lt;/ul&gt;
                          &lt;?php   
                          }
                          ?&gt;
                      &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
      &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Output of the above code is <a href="http://brandsneed.com/btree/" rel="nofollow noreferrer">Here</a>,</p>
  
  <p>From database table, <strong>PX103</strong> is a parent of PX108 and PX110, <strong>PX105</strong> is parent of PX107 and PX111 , <strong>PX106</strong> is parent of PX109. But PX108, PX110, PX107, PX111 and PX109 are not displaying in tree.</p>
  
  <p>Please help me to sort out this problem. 
  Thankyou. </p>