<?php
Class Database { }
Class Team extends Database {
private $dbConnection;
function __construct($db)
{
$this->dbConnection = $db;
}
public function getDownline($id, $depth=5) {
$stack = array([$id]);
for($i=1; $i<=$depth; $i++) {
//echo $i.PHP_EOL;
// create an array of levels, each holding an array of child ids for that level
$stack[$i] = $this->getChildren($stack[$i-1]);
}
return $stack;
}
public function countLevel($level) {
// expects an array of child ids
settype($level, 'array');
return sizeof($level);
}
private function getChildren($parent_ids = array()) {
//var_dump($parent_ids);
$results = array();
$placeholders = str_repeat('?,', count($parent_ids) - 1). '?';
$sql="select id from users where pid in ($placeholders)";
$stmt=$this->dbConnection->prepare($sql);
$stmt->execute($parent_ids);
while($row=$stmt->fetch()) {
$results[] = $row["id"];
}
return $results;
}
}
$getTeam = new Team($pdo);
$id = 1;
$depth = 3; // get the counts of his downline, only 2 deep.
$downline_array = $getTeam->getDownline($id, $depth=3);
echo "Results: ";
var_dump($downline_array);