我试图关注如何在PHP中进行Treeview的一些在线网站。我有它的工作,但我有大量的MySQL调用,以使其工作。我是PHP的新手,我知道必须有更好的方法。任何帮助都会很棒。
$childid = '';
$parent = DB::table('ezygold_users')->join('ezygold_usersplan', 'ezygold_users.id', '=', 'ezygold_usersplan.idusrpp')->where('id', '=', $id->users_id)->select('id', 'fullname', 'idref', 'idspr')->get();
foreach ($parent as $parentout) {
echo $parentout->fullname;
}
echo '<ol>';
foreach (DB::table('ezygold_users')->join('ezygold_usersplan', 'ezygold_users.id', '=', 'ezygold_usersplan.idusrpp')->where('idref', '=', $parentout->id)->select('id', 'fullname', 'idref', 'idspr')->get() as $child) {
echo '<li>' . $child->fullname . '</li>';
$childid = $child->id;
echo '<ol>';
foreach (DB::table('ezygold_users')->join('ezygold_usersplan', 'ezygold_users.id', '=', 'ezygold_usersplan.idusrpp')->where('idref', '=', $childid)->select('id', 'fullname', 'idref', 'idspr')->get() as $child) {
echo '<li>' . $child->fullname . '</li>';
$childid = $child->id;
echo '<ol>';
foreach (DB::table('ezygold_users')->join('ezygold_usersplan', 'ezygold_users.id', '=', 'ezygold_usersplan.idusrpp')->where('idref', '=', $childid)->select('id', 'fullname', 'idref', 'idspr')->get() as $child) {
echo '<li>' . $child->fullname . '</li>';
$childid = $child->id;
echo '<ol>';
foreach (DB::table('ezygold_users')->join('ezygold_usersplan', 'ezygold_users.id', '=', 'ezygold_usersplan.idusrpp')->where('idref', '=', $childid)->select('id', 'fullname', 'idref', 'idspr')->get() as $child) {
echo '<li>' . $child->fullname . '</li>';
$childid = $child->id;
}
echo '</ol>';
}
echo '</ol>';
}
echo '</ol>';
}
echo '</ol>';
编辑:
我知道这一点,但回归大约需要30至45秒。你能明白为什么吗?
function getTree($id)
{
$child = DB::table('ezygold_users')->join('ezygold_usersplan', 'ezygold_users.id', '=', 'ezygold_usersplan.idusrpp')->select('id', 'fullname', 'idref', 'idspr', 'idusrpp')->get();
echo '<ul>';
foreach ($child as $childout) {
$rollup = DB::table('ezygold_users')->where('id', '=',$childout->idspr)->first();
if ($childout->idref == $id) {
echo '<li>' . $childout->fullname . '<br>' . '( ' . $rollup->fullname . ' )</li>';
$this->getTree($childout->id);
}
}
echo '</ul>';
return;