PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE users (id int, pid int, name varchar(100)); INSERT INTO users (id, pid, name) VALUES (1, 0, 'Steven Rogers'), (2, 1, 'Bruce Banner'), (3, 1, 'Wally West'), (4, 2, 'Peter Parker'), (5, 2, 'Jay Garrick'), (6, 4, 'Barry Allen'), (7, 4, 'Reed Richards'); SELECT * FROM users;
Copy Clear
Copy Format Clear
<?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 = 2; // get the counts of his downline, only 2 deep. $downline_array = $getTeam->getDownline($id, $depth=2); echo "Results: "; var_dump($downline_array);
Show:  
Copy Clear