递归Php函数不起作用

时间:2017-11-03 13:41:43

标签: php function recursion jstree

我试图在表中显示来自作为参数传递的ID的所有子节点。我在PHP中使用了一个递归函数来做到这一点。 唯一的问题是它只显示第一个孩子而不是所有孩子。

This is the Tree.

PHP函数:

$data = array();
$index = array();
$albero = array();
$query = $conn->query("SELECT id, insertedby FROM utenti where insertedby is not null ORDER BY id");
$query->data_seek(0);
while ($row = $query->fetch_assoc()) {
    $id        = $row["id"];
    $parent_id = $row["insertedby"] === NULL ? "NULL" : $row["insertedby"];
    $data[$id] = $row;
    $index[$parent_id][] = $id;
}

function findNodeLevel($parent_id, $level, $idtosearch)
{
    global $data, $index;
    $parent_id = $parent_id === NULL ? "NULL" : $parent_id; 
    print_r($data);
    if (isset($index[$parent_id])) {
        foreach ($index[$parent_id] as $id) {
            $current_id = $data[$id]["id"];
            if($current_id == $idtosearch) {
                $levell = $level + 1;
                return $levell;
            }
            findNodeLevel($_SESSION['id'], $level + 1, $idtosearch);
        }
    }
}

结果如下:

Result of the function

第二张图片中显示的数组数据是$ data数组。

while循环:

$result = $conn->query("SELECT id, email, tipoutente, fullname, datainserimento FROM utenti WHERE inseritoda = " . $_SESSION['id']);
        while($row = mysqli_fetch_array($result)){
            echo "<tr class=\"righe\">";

                if($row['tipoutente'] == "GRUPPO PRIVATO" || $row['tipoutente'] == "GRUPPO NEGOZI" || $row['tipoutente'] == "GRUPPO ASSOCIAZIONI") {
                    $livello = findNodeLevel($_SESSION['id'], 0, $row['id']);
                    echo "<td class=\"gruppi\">$progressivoutenti</td>";
                    echo "<td class=\"gruppi\">" . $row['id'] . "</td>";
                    echo "<td class=\"gruppi\">" . $row['fullname'] . "</td>";
                    echo "<td class=\"gruppi\">" . $row['email'] . "</td>";
                    echo "<td class=\"gruppi\">" . $row['tipoutente'] . "</td>";
                    echo "<td class=\"gruppi\">" . $row['datainserimento'] . "</td>";
                    //echo "<td class=\"gruppi\">" . $row['datadiff'] . "</td>";
                    echo "<td class=\"gruppi\">" . $livello . "</td>";
                } else {
                    $livello = findNodeLevel($_SESSION['id'], 0, $row['id']);
                    echo "<td class=\"dati\">$progressivoutenti</td>";
                    echo "<td class=\"dati\">" . $row['id'] . "</td>";
                    echo "<td class=\"dati\">" . $row['fullname'] . "</td>";
                    echo "<td class=\"dati\">" . $row['email'] . "</td>";
                    echo "<td class=\"dati\">" . $row['tipoutente'] . "</td>";
                    echo "<td class=\"dati\">" . $row['datainserimento'] . "</td>";
                    //echo "<td class=\"dati\">" . $row['datadiff'] . "</td>";
                    echo "<td class=\"dati\">" . $livello . "</td>";
                }

                $progressivoutenti++;
            echo "</tr>";   

        }
    echo "</table>";

1 个答案:

答案 0 :(得分:0)

在递归函数中,当你再次调用它时,是否为parent_id传递了错误的参数?

function findNodeLevel($parent_id, $level, $idtosearch)
{
    global $data, $index;
    $parent_id = $parent_id === NULL ? "NULL" : $parent_id; 
    print_r($data);
    if (isset($index[$parent_id])) {
        foreach ($index[$parent_id] as $id) {
            $current_id = $data[$id]["id"];
            if($current_id == $idtosearch) {
                $levell = $level + 1;
                return $levell;
            }
            findNodeLevel($current_id, $level + 1, $idtosearch);
        }
    }
}