I would like to make a function in PHP that runs through the entire binary tree, so I made this code.
function getTree($id, $count, $conn) {
if ((!isset($count)) || ($count = null)) {
$count = 1;
}
$query = $conn->prepare("SELECT login FROM usuarios WHERE ID = :id");
$query->execute(array("id" => $id));
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
if (count($rows) > 0) {
for ($i = 0; $i < $count; $i++) {
echo "-";
}
echo $rows[0]["login"]."<br />";
$query = $conn->prepare("SELECT id FROM dados WHERE upline = :id");
$query->execute(array("id" => $id));
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row) {
$nivel++;
getTree($row["id"], $count + 1, $conn);
}
}
}
It catches all the people that are there, perfect. But I would like it to search as the image shows, but by looking at the image below, 1, 2, 4, 8, 16, 17, 9, 18, 19, and so on.
The function would look for the second line (numbers 2 and 3) and based on id 1; Then it would return and run with Id 4 and 5, related to 2, and 6 and 6 related to 3 and so on.