Trying to create a binary tree but my downline is not in the correct order
<p>I am trying to create a binary tree using PHP, MySQL and a <a href="http://frank-mich.com/jQuery/" rel="nofollow noreferrer">jQuery plugin by Frank-Mich</a></p>

<p>This is what I have so far...</p>

<p><strong>DATABASE STRUCTURE</strong></p>

<pre class="lang-sql prettyprint-override"><code>CREATE TABLE IF NOT EXISTS `members` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `regDate` date NOT NULL,
  `memberID` varchar(20) NOT NULL,
  `sponsor` int(10) NOT NULL,
  `position` varchar(5) NOT NULL,
  `firstname` varchar(50) NOT NULL,
  `lastname` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;

INSERT INTO `members` (`id`, `regDate`, `memberID`, `sponsor`, `position`, `firstname`, `lastname`) VALUES
(1, '2016-10-01', 'S0000000', 0, '', 'Fred', NULL),
(2, '2016-10-02', 'S5483002', 1, '', 'John', NULL),
(3, '2016-10-03', 'S6182013', 2, 'Lt', 'Innorlito', NULL),
(4, '2016-10-03', 'S6325681', 18, 'Lt', 'Sarah', NULL),
(5, '2016-10-04', 'S6329592', 3, 'Lt', 'Imelda', NULL),
(6, '2016-10-06', 'S6345140', 5, 'Rt', 'Diane', NULL),
(7, '2016-10-05', 'S6325799', 4, 'Lt', 'John III', NULL),
(8, '2016-10-05', 'S6356038', 4, 'Rt', 'Myda', NULL),
(9, '2016-10-07', 'S6358217', 7, 'Lt', 'Rondale', NULL),
(10, '2016-10-07', 'S6506318', 7, 'Rt', 'Rosana', NULL),
(11, '2016-10-08', 'S6356151', 8, 'Lt', 'Wiljames', NULL),
(12, '2016-10-08', 'S6456156', 8, 'Rt', 'Mark', NULL),
(13, '2016-10-09', 'S6379249', 16, 'Rt', 'Maria Fe', NULL),
(14, '2016-10-09', 'S6345177', 6, 'Lt', 'Penafrancia', NULL),
(15, '2016-10-10', 'S6345204', 6, 'Rt', 'Ruth', NULL),
(16, '2016-10-04', 'S6345083', 5, 'Lt', 'Maricel', NULL),
(17, '2016-10-11', 'S6345905', 16, 'Lt', 'Elena', NULL),
(18, '2016-10-18', 'S6320374', 2, 'Rt', 'Myrna', NULL),
(19, '2016-10-19', 'S6383350', 3, 'Rt', 'Reynaldo', NULL),
(20, '2016-10-17', 'S6383593', 19, 'Lt', 'Narcisa', NULL),
(21, '2016-10-17', 'S6510270', 19, 'Rt', 'Juanita', NULL),
(22, '2016-11-02', 'S6384918', 20, 'Lt', 'Arsenia', NULL),
(23, '2016-11-02', 'S6385105', 20, 'Rt', 'Zusara', NULL),
(24, '2016-11-04', 'S6511807', 21, 'Lt', 'Vivian', NULL);
</code></pre>

<ul>
<li><code>memberID</code> - is alpha-numeric, used for display only</li>
<li><code>sponsor</code> - is the "parent ID" for that child record</li>
<li><code>position</code> - is the placement in the binary structure, Left or Right</li>
</ul>

<p><strong>PHP</strong></p>

<pre><code>if(!isset($_SESSION['MM_id']) || $_SESSION['MM_id'] == '') {
    $id=2;
} else {
    $id = $_SESSION['MM_id'];
}

// pull the "head" record... "top" of the binary
$sql = "SELECT * FROM members WHERE `id`=:id";
$query = $conn-&gt;prepare($sql);
$query-&gt;bindValue(':id', $id, PDO::PARAM_INT);
$query-&gt;execute();
$result = $query-&gt;fetch(PDO::FETCH_ASSOC);
$totalRows = $query-&gt;rowCount();

// pull all records (children) of the top record and group them into their parent-child relationships
$sql = "SELECT * FROM members WHERE `id` &gt; :id ORDER BY `sponsor` ASC, `id` ASC";
$query_rsMembers = $conn-&gt;prepare($sql);
$query_rsMembers-&gt;bindValue(':id', $id, PDO::PARAM_INT);
$query_rsMembers-&gt;execute();
$result_rsMembers = $query_rsMembers-&gt;fetch(PDO::FETCH_ASSOC);
$totalRows_rsMembers = $query_rsMembers-&gt;rowCount();

$tree = ''; // create an empty tree variable
$used = ''; // create an empty used variable
//echo 'This is the order that the records were pulled&lt;br&gt;';
do {
    $sid = $result_rsMembers['sponsor'];
    $sql = "SELECT * FROM members WHERE `sponsor` = :sid AND `id` != :used ORDER BY `position`";
    $query_rsTree = $conn-&gt;prepare($sql);
    $query_rsTree-&gt;bindValue(':sid', $sid, PDO::PARAM_STR);
    $query_rsTree-&gt;bindValue(':used', $used, PDO::PARAM_INT);
    $query_rsTree-&gt;execute();
    $result_rsTree = $query_rsTree-&gt;fetch(PDO::FETCH_ASSOC);
    $totalRows_rsTree = $query_rsTree-&gt;rowCount();
    $used = $result_rsTree['id'];
    $tree .= '&lt;div align="center" style="border-style: inset; margin:0 0 10px 10px; font-family:Arial, Helvetica, sans-serif; font-size:12px;"&gt;&lt;span&gt;'.$result_rsTree['firstname'].'&lt;br&gt;'.$result_rsTree['memberID'].'&lt;br&gt;'.$result_rsTree['regDate'].'&lt;/span&gt;&lt;/div&gt;';
    //echo $result_rsTree['firstname'].'&lt;br&gt;';
} while ($result_rsMembers = $query_rsMembers-&gt;fetch(PDO::FETCH_ASSOC));

$timenow = time() + 28800;
</code></pre>

<p><strong>HTML</strong></p>

<pre><code>&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;Binary Tree&lt;/title&gt;
&lt;script type="text/javascript" src="inc/wz_jsgraphics.js?v=&lt;?php echo $timenow ?&gt;"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="../js/jquery/jquery-1.12.4.js?v=&lt;?php echo $timenow ?&gt;"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="inc/jquery.btree.js?v=&lt;?php echo $timenow ?&gt;"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
var myTree;
$(document).ready(function(){
    myTree = $("#treeDiv").btree()[0];
});

function mich(){
    $("#box1").css("left", "");
    alert($("#box1").css("left") + " - " + $("#box1").css("right"));
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="treeDiv" style="vertical-align:text-top; overflow:hidden; min-height:400px; min-width:1000px; border-style:solid; border-width: 3px; border-color: #000000;"&gt;
    &lt;?php echo $tree ?&gt;
    &lt;div align="center" style="border-style: inset; margin:0 0 10px 10px; font-family:Arial, Helvetica, sans-serif; font-size:12px;" id="box1"&gt;
        &lt;span&gt;&lt;?php echo $result['firstname'].'&lt;br&gt;'.$result['memberID'].'&lt;br&gt;'.$result['regDate'] ?&gt;&lt;/span&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>The problem I am having is creating the tree in the proper order of parent-child (left) and parent-child right.</p>

<p>This image shows the way that the tree is created...</p>

<p><a href="https://i.stack.imgur.com/SnJkl.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SnJkl.jpg" alt=""></a></p>

<p>This is the way it should look...</p>

<p><a href="https://i.stack.imgur.com/QEa6m.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QEa6m.jpg" alt=""></a></p>

<p>It appears to be pulling the records in the correct order and grouping them in their correct parent child relationship; however, when it begins to build the tree it simply creates left floating divs starting from left to right.</p>

<p>Try as I may, I cannot seem to get them display correctly.</p>

<p>I hope someone can help with this.</p>