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