4

基于:将修改后的前序树遍历模型(嵌套集)放入 <ul>

我有下表:

id  name                            lft rgt level
1   company                         1   22  0
75  Developer                       26  31  1
76  Tester                          24  27  1
77  Analyst                         22  23  1
78  under developer                 27  30  2
79  under tster                     25  26  2

以及以下用于获取嵌套记录的查询/代码:

    function getstructureInformation() {
        $treeArr = array();
        $tree = array();
        $sql = "SELECT node.name, node.id, node.unit_id,
               node.description,node.lft,node.rgt,node.level, 
               (COUNT(parent.name) - 1) AS depth
               FROM tablename AS node
               CROSS JOIN tablename AS parent
               WHERE node.lft BETWEEN parent.lft AND parent.rgt
               GROUP BY node.name
               ORDER BY node.lft";

        $query = $this->db->query($sql);
        $data = $query->result();

        foreach ($data as $datap) {
            $treeArr['id'] = $datap->id;
            $treeArr['unit_id'] = $datap->unit_id;
            $treeArr['lft'] = $datap->lft;
            $treeArr['rgt'] = $datap->rgt;
            $treeArr['level'] = $datap->level;
            $treeArr['name'] = $datap->name;
            $treeArr['depth'] = $datap->depth;
            $treeArr['description'] = $datap->description;
            $tree[] = $treeArr;
        }
        return $tree;
    }

这里是用于将其显示到视图页面中的 PHP 代码:

$result = '';
 $currDepth = -1;  // -1 to get the outer <ul>
 while (!empty($tree)) {
  $currNode = array_shift($tree);
  if ($currNode['depth'] > $currDepth) {
    echo '<ul>';
  }
  if ($currNode['depth'] < $currDepth) {       
    $result .= str_repeat('</ul>', $currDepth - $currNode['depth']);
  }
  echo '<li>' . $currNode['name'] . '</li>';
  $currDepth = $currNode['depth'];
  if (empty($tree)) {
    echo str_repeat('</ul>', $currDepth + 1);
  }
}

但它无法正确显示,例如:

它显示输出为:

001 : Company Name

    3 : Analyst      

2 : Tester      

    33 : under tster      
        1 : Developer      
        44 : under developer

所需的输出如下:

001 : Company Name

    3 : Analyst  
    2 : Tester  
        33 : under tster  
    1 : Developer  
        44 : under developer

有什么解决办法吗?

4

1 回答 1

3

这应该工作:

function printTree ($tree) {
    $last_level = -1;

    foreach ($tree as $v) {
        $diff = $v['level'] - $last_level;
        if ($diff == 0)  {
            echo '<li>' .$v['name']. '</li>';
        }
        elseif ($diff > 0) {
            for ($i = 0; $i < $diff; $i++)
                echo '<ul>';
            echo '<li>' .$v['name']. '</li>' ;
        }
        else {
            for ($i = 0; $i > $diff; $i--)
                echo '</ul>';
            echo '<li>' .$v['name']. '</li>' ;
        }
        $last_level = $v['level'];
    }
}

但是使用此功能,您将不会打印深度。

经过测试和使用

$tree = array (
    array (
        'name' => 'Line',
        'level' => 0
    ),
    array (
        'name' => 'Line',
        'level' => 1
    ),
    array (
        'name' => 'Line',
        'level' => 2
    ),
    array (
        'name' => 'Line',
        'level' => 1
    )
);
printTree ($tree);
于 2012-08-03T09:28:14.900 回答