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

    前端 未结 7 1214
    傲寒
    傲寒 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:14

      There's a PEAR package for dealing with nested sets: DB_NestedSet.
      You might also be interested in the article Managing Hierarchical Data in MySQL.

      0 讨论(0)
    • 2020-11-27 11:19

      i`m using CROSS JOIN query displaying jsTree jQuery menu; Everything works just great ! The existing table I added a column for the position. However, when I define position and ordered all by position, the corresponding items are not grouped properly. I guess it's a query issue, tried many combinations, but no success.

      0 讨论(0)
    • 2020-11-27 11:21

      This should be what you're looking for:

      function getCats($left = null, $right = null)
      {
          $sql = array();
          $result = null;
      
          if (isset($left) === true)
          {
              $sql[] = 'lft >= ' . intval($left);
          }
      
          if (isset($right) === true)
          {
              $sql[] = 'rght <= ' . intval($right);
          }
      
          if (empty($sql) === true)
          {
              $sql[] = 'lft = 1';
          }
      
          $sql = 'SELECT * FROM t_categories WHERE ' . implode(' AND ', $sql) . ';';
      
          if ($rs = C_DB::fetchRecordset($sql))
          {
              // you need to make sure that the query returns
              // something to correctly display the ULs
              if (empty($rs) === false)
              {
                  $result .= '<ul>' . "\n";
      
                  while ($row = C_DB::fetchRow($rs))
                  {
                      $result .= '<li>' . $row['title'] . '</li>' . "\n";
                      $result .= getCats($row['lft'], $row['rght']);
                  }
      
                  $result .= '</ul>' . "\n";
              }
          }
      
          return $result;
      }
      

      To get the HTML for your nested tree you should do:

      echo getCats();
      

      Please note that your nested set sample doesn't look right, also you should make sure if I didn't made any mistake invoking your C_DB class, I don't know since I'm not familiarized with it.

      0 讨论(0)
    • 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 = "<ul>";
      $currDepth = 0;
      
      while($row = mysql_fetch_assoc($query_result))
      {
        if($row['depth'] > $currDepth)
        {
          $result .= "<li><ul>"; // open sub tree if level up
        }
      
        if($row['depth'] < $currDepth)
        {
          $result .= str_repeat("</ul></li>", $currDepth - $row['depth']); // close sub tree if level down
        }
      
        $result .= "<li>$row['name']</li>"; // Always add node
        $currDepth = $row['depth'];
      }
      $result .= "</ul>";
      
      echo $result;
      
      0 讨论(0)
    • 2020-11-27 11:26

      Ok, let's do some bounty hunting ;)

      Step 0 - Sanitize example:
      As already mentioned, your example data is broken, as it does not define a valid nested set. If you took this data from an app, you should check the insert/delete logic.

      So for testing, I used a sanitized version like so:
      (MySQL here, as it was the first at hand)

      CREATE TABLE t_categories`(
        `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
        `title` VARCHAR(45) NOT NULL,
        `lft` INTEGER UNSIGNED NOT NULL,
        `rght` INTEGER UNSIGNED NOT NULL,
        PRIMARY KEY (`id`)
      );
      
      INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 1',1,16);
      INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 2',2,3);
      INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 3',4,7);
      INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 4',5,6);
      INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 5',8,13);
      INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 6',9,12);
      INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 7',10,11);
      INSERT INTO t_categories (title, lft, rght) VALUES ('Cat 8',14,15);
      

      Step 1 - Let the database do the ordering
      Nested sets where primarily invented as a convenient way of storing trees in databases, as they make it pretty easy to query for subtrees, parent relations and, especially interesting in this case, for order and depth:

      SELECT node.title, (COUNT(parent.title) - 1) AS depth
       FROM t_categories AS node
       CROSS JOIN t_categories AS parent
       WHERE node.lft BETWEEN parent.lft AND parent.rght
       GROUP BY node.title
       ORDER BY node.lft
      

      This will return your set neatly ordered, starting with the root node and continuing to the end in preorder. Most importantly, it will add the depth of each node as a positive integer, indicating how many levels the node is below root (level 0). For the above example data, the result will be:

      title, depth
      'Cat 1', 0
      'Cat 2', 1
      'Cat 3', 1
      'Cat 4', 2
      'Cat 5', 1
      'Cat 6', 2
      'Cat 7', 3
      'Cat 8', 1
      

      In code:

      // Grab ordered data
      $query = '';
      $query .= 'SELECT node.title, (COUNT(parent.title) - 1) AS depth';
      $query .= ' FROM t_categories AS node';
      $query .= ' CROSS JOIN t_categories AS parent';
      $query .= ' WHERE node.lft BETWEEN parent.lft AND parent.rght';
      $query .= ' GROUP BY node.title';
      $query .= ' ORDER BY node.lft';
      
      $result = mysql_query($query);
      
      // Build array
      $tree = array();
      while ($row = mysql_fetch_assoc($result)) {
        $tree[] = $row;
      }
      

      The resulting array will look like this:

      Array
      (
          [0] => Array
              (
                  [title] => Cat 1
                  [depth] => 0
              )
      
          [1] => Array
              (
                  [title] => Cat 2
                  [depth] => 1
              )
          ...
      )
      

      Step 2 - Output as HTML list fragment:

      Using while loop:

      // bootstrap loop
      $result = '';
      $currDepth = -1;  // -1 to get the outer <ul>
      while (!empty($tree)) {
        $currNode = array_shift($tree);
        // Level down?
        if ($currNode['depth'] > $currDepth) {
          // Yes, open <ul>
          $result .= '<ul>';
        }
        // Level up?
        if ($currNode['depth'] < $currDepth) {
          // Yes, close n open <ul>
          $result .= str_repeat('</ul>', $currDepth - $currNode['depth']);
        }
        // Always add node
        $result .= '<li>' . $currNode['title'] . '</li>';
        // Adjust current depth
        $currDepth = $currNode['depth'];
        // Are we finished?
        if (empty($tree)) {
          // Yes, close n open <ul>
          $result .= str_repeat('</ul>', $currDepth + 1);
        }
      }
      
      print $result;
      

      Same logic as recursive function:

      function renderTree($tree, $currDepth = -1) {
        $currNode = array_shift($tree);
        $result = '';
        // Going down?
        if ($currNode['depth'] > $currDepth) {
          // Yes, prepend <ul>
          $result .= '<ul>';
        }
        // Going up?
        if ($currNode['depth'] < $currDepth) {
          // Yes, close n open <ul>
          $result .= str_repeat('</ul>', $currDepth - $currNode['depth']);
        }
        // Always add the node
        $result .= '<li>' . $currNode['title'] . '</li>';
        // Anything left?
        if (!empty($tree)) {
          // Yes, recurse
          $result .=  renderTree($tree, $currNode['depth']);
        }
        else {
          // No, close remaining <ul>
          $result .= str_repeat('</ul>', $currNode['depth'] + 1);
        }
        return $result;
      }
      
      print renderTree($tree);
      

      Both will output the following structure:

      <ul>
          <li>Cat 1</li>
          <li>
              <ul>
                  <li>Cat 2</li>
                  <li>Cat 3</li>
                  <li>
                      <ul>
                          <li>Cat 4</li>
                      </ul>
                  </li>
                  <li>Cat 5</li>
                  <li>
                      <ul>
                          <li>Cat 6</li>
                          <li>
                              <ul>
                                  <li>Cat 7</li>
                              </ul>
                          </li>
                      </ul>
                  </li>
                  <li>Cat 8</li>
              </ul>
          </li>
      </ul>
      

      Nitpickers corner: Questioner explicitly asked for <ul>, but ordered unordered lists!? Come on...
      ;-)

      0 讨论(0)
    • 2020-11-27 11:28
      $linaje='';
          $lastnode='';
          $sides['izq']=array();
          $sides['der']=array();
          $print = '<ul>'; 
          foreach ($array as $key1 => $value1){ //Proyectos
      
              if(strpos($info[$key1]['linaje'],'-') !== false)
                  $compare = strstr($info[$key1]['linaje'],'-',true);
              else
                  $compare  = $info[$key1]['linaje'];
      
              if($linaje != ''){
                  if ($linaje !=   $compare){
                      $linaje= $compare;
                      $sides['izq']=array();
                      $sides['der']=array();
                      //for($i=1;$i <= substr_count($lastnode,'`')-substr_count($value1,'`');$i++)
                          //$print .= '</ul></li>';
                  }
              }
      
      
              if ($lastnode != '')
                  for($i=1;$i<= substr_count($lastnode,'`')-substr_count($value1,'`');$i++)
                      $print .= '</ul></li>'; 
      
              if (count($sides['der'])>0)
                  if  ($sides['der'][count($sides['der'])-1] > $info[$key1]['der'])
                      $print .= '<ul>';
      
              $print .= '<li><a href="#'.$info[$key1]['id'].'#'.$info[$key1]['linaje'].'">'.substr($value1,substr_count($value1,'`')).'</a>';
      
              if  ($info[$key1]['der'] - $info[$key1]['izq'] == 1)
                      $print .= '</li>';
      
              if ($key1 == count($info)-1)
                  for($i=1;$i <= substr_count($lastnode,'`')-1;$i++)
                      $print .= '</ul></li>';
      
              $sides['der'][] = $info[$key1]['der'];
              $sides['izq'][] = $info[$key1]['izq'];
      
              if ($linaje =='')
                      $linaje = $info[$key1]['linaje'];
      
              $lastnode = $value1;
          }
          $print .= '</ul>';
          echo $print;
      

      the difference in this is that you can render X numbers of trees, this applies to one of my projects. and I use a char as a depth reference when I fetch the rows from the DB

      0 讨论(0)
    提交回复
    热议问题