【发布时间】:2013-03-13 22:48:01
【问题描述】:
我有一个包含 2 列用户名和领导者的表
login_user sponsered_id right_left
test1 admin Right
test2 admin Left
test3 test1 Right
test4 test1 Left
test43 test2 Left
test44 test3 Left
我有一个功能
function display_children($parent, $level) {
// retrieve all children of $parent
$result = mysql_query('SELECT name, login_user, right_left FROM members_list '.
'WHERE sponsered_id="'.$parent.'";');
while ($row = mysql_fetch_array($result)) {
// indent and display the title of this child
echo '<tr><td>'.
$row['login_user'].' </td><td> '.$row['right_left'].' </td><td> '.$row['sponsered_id'].
"</td></tr>";
// call this function again to display this
// child's children
display_children($row['login_user'], $level+1);
}
}
echo display_children('admin',0);
但没有得到正确的输出....它给了我输出
test1 Right admin
test3 Right test1
test44 Left test3
test4 Right test1
test2 Left admin
test43 Left test2
需要输出为
test1 Right admin
test2 Left admin
test3 Right test1
test4 Left test1
Right test2
test43 Left test2
Right test3
test44 Left test3
【问题讨论】:
-
您所说的输出与数据库中的值不匹配:
Right test2不存在。我认为您应该只通过 login_user 订购它们,并且您应该非常接近您所说的输出。 -
我想正如 Kao 所建议的,对结果集进行排序可能会给您带来更理想的输出。另外,我想推荐使用嵌套集模型层次结构数据,它更容易使用和遍历层次结构中的槽节点和分支。
-
@Kao 那就是我的问题是某些值不在左右,它应该显示空白......
标签: php mysql hierarchical