Getting a modified preorder tree traversal model (nested set) into a

    前端 未结 7 1395
    傲寒
    傲寒 2020-11-27 10:56

    I am trying to get my data which is hierarchically set up with a tree traversal model into an < ul> in order to show on my site.

    Here is my code:

    
    
            
    7条回答
    •  不知归路
      2020-11-27 11:21

      Simply loop thru the result will do:

      $sql = "SELECT node.name, (COUNT(parent.name) - 1) AS depth
              FROM nested_category AS node,
              nested_category AS parent
              WHERE node.lft BETWEEN parent.lft AND parent.rgt
              GROUP BY node.name
              ORDER BY node.lft";
      
      $query_result = mysql_query($sql)
      
      $result = "
        "; $currDepth = 0; while($row = mysql_fetch_assoc($query_result)) { if($row['depth'] > $currDepth) { $result .= "
        • "; // open sub tree if level up } if($row['depth'] < $currDepth) { $result .= str_repeat("
      • ", $currDepth - $row['depth']); // close sub tree if level down } $result .= "
      • $row['name']
      • "; // Always add node $currDepth = $row['depth']; } $result .= "
      "; echo $result;

    提交回复
    热议问题