id = $id; $this->parentid = $parentid; $this->typ = $typ; $this->children = array(); } }; function loadall() { $list = array(); $con = new mysqli('localhost', 'root', '', 'Test'); $stmt = $con->prepare('SELECT id,parentid,typ FROM treedemo'); $stmt->execute(); $stmt->store_result(); $stmt->bind_result($id, $parentid, $typ); while($stmt->fetch()) { $list[$id] = new Record($id, $parentid, $typ); if($parentid != null) $list[$parentid]->children[] = $id; } $stmt->close(); $con->close(); return $list; } function load_children($currentid, $all, &$list) { $folders = array(); foreach($all[$currentid]->children as $childid) { $child = $all[$childid]; if($child->typ == 'Content') { $list[] = $child; } else if($child->typ == 'Folder') { $folders[] = $child->id; } } foreach($folders as $folderid) { load_children($folderid, $all, $list); } } function load($startid, $all) { $list = array(); $children = false; if(array_key_exists($startid, $all)) { $startrec = $all[$startid]; if($startrec->typ == 'Content') { $list[] = $startrec; } else if($startrec->typ == 'Folder') { $children = true; } } if($children) { load_children($startid, $all, $list); } return $list; } $all = loadall(); $all3 = load(3, $all); foreach($all3 as $one3) { echo $one3->id . ' ' . $one3->typ . "
\r\n"; } $all8 = load(8, $all); foreach($all8 as $one8) { echo $one8->id . ' ' . $one8->typ . "
\r\n"; } $all9 = load(9, $all); foreach($all9 as $one9) { echo $one9->id . ' ' . $one9->typ . "
\r\n"; } $t1 = microtime(true); for($i = 0; $i < 1000; $i++) { load(3, $all); } $t2 = microtime(true); echo ($t2 - $t1) . "
\r\n"; ?>