-1

我们在一个社交项目上有一个成员数据库,其中包括哪个成员推荐了另一个成员。数据库的字段如下所示:

id | name | email | code | recruit_by

现在我们要打印结构的嵌套列表,谁在所有深层推荐谁。

我们没有使用以下代码运行它:

<?PHP

mysql_connect("www.mysqlserver.net", "database1", "password") or die(mysql_error());
mysql_select_db("project_db1") or die(mysql_error());

echo "<ul>";

$result = mysql_query("SELECT * FROM registration") or die(mysql_error());
while($row = mysql_fetch_array($result))
    {
    echo "<li class=\"level0\">" . $row['id'] . " - " . $row['name'] . " - " . $row['email'] . " - " . $row['recruit_by'] . "</li>";

    // 1. Level
    $result2 = mysql_query("SELECT * FROM registration WHERE recruit_by LIKE " . $row['id']) or die(mysql_error());
    while($row2 = mysql_fetch_array($result2))
        {
        echo "<li class=\"level1\">1. " . $row2['id'] . " - " . $row2['name'] . " - " . $row2['email'] . " - " . $row2['recruit_by'] . "</li>";

        // 2. Level
        $result3 = mysql_query("SELECT * FROM registration WHERE recruit_by LIKE " . $row2['id']) or die(mysql_error());
        while($row3 = mysql_fetch_array($result3))
            {
            echo "<li class=\"level2\">2. " . $row3['id'] . " - " . $row3['name'] . " - " . $row3['email'] . " - " . $row3['recruit_by'] . "</li>";

            // 3. Level
            $result4 = mysql_query("SELECT * FROM registration WHERE recruit_by LIKE " . $row3['id']) or die(mysql_error());
            while($row4 = mysql_fetch_array($result4))
                {
                echo "<li class=\"level3\">3. " . $row4['id'] . " - " . $row4['name'] . " - " . $row4['email'] . " - " . $row4['recruit_by'] . "</li>";

                // 4. Level
                $result5 = mysql_query("SELECT * FROM registration WHERE recruit_by LIKE " . $row4['id']) or die(mysql_error());
                while($row5 = mysql_fetch_array($result5))
                    {
                    echo "<li class=\"level4\">4. " . $row5['id'] . " - " . $row5['name'] . " - " . $row5['email'] . " - " . $row5['recruit_by'] . "</li>";

                    }

                }

            }

        }

    }

echo "</ul>";

?>
4

1 回答 1

0

非常类似于我在这里给出的答案: MySQL 最佳实践:SELECT children recursive as possible?

同样不是 MySQL 的最佳用例,但是是的....

不要做这么多查询,做一个“SELECT * FROM registration”

通过结果将其构建为一棵树,例如...

// use a database query to get this member info, here a simplified version
$member = array();
$member[] = array('id' =>'2', 'recruit_by' =>'1');
$member[] = array('id' =>'3', 'recruit_by' =>'2');
$member[] = array('id' =>'4', 'recruit_by' =>'3');
$member[] = array('id' =>'9', 'recruit_by' =>'1');

//use php to build a array containing all the recruit-relationships as a tree
function buildtree($member) {
    $ref = array();
    foreach($member as $mem){
        $member_id = $mem['id'];
        $parent = $mem['recruit_by'];
        if(!is_array($ref[$member_id])) $ref[$member_id] = array('id' => $member_id);
        if(!is_array($ref[$parent])) $ref[$parent] = array('id' => $parent);
        $ref[$parent]['child'][] = &$ref[$member_id];
    }
    return $ref;
}

$tree = buildtree($member);

/// use a recursive function to output the tree
function echotree($tree, $parent = 0) {
    foreach ($tree as $t) {
        if($parent){
            echo "$parent recruited " . $t['id'] . '<br>';

        }
        if(is_array($t['child']) && !$parent){
            echotree($t['child'],$t['id']);
        }
    }
}

echotree($tree);

会给你:

2 recruited 3
1 recruited 2
1 recruited 9
3 recruited 4

不应该太难把它放在你想要的任何布局中。

于 2013-09-21T21:15:31.130 回答