-7

DB structure is this:

INSERT INTO categories (title, lft, rgt) VALUES ('Cat 1',1,16);
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 2',2,3);
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 3',4,7);
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 4',5,6);
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 5',8,13);
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 6',9,12);
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 7',10,11);
INSERT INTO categories (title, lft, rgt) VALUES ('Cat 8',14,15);  

When I run this query:

SELECT n.title, COUNT(*)-1 AS depth FROM categories AS n, categories AS p WHERE  n.lft BETWEEN p.lft AND p.rgt GROUP BY n.lft ORDER BY n.lft;

$result = mysql_query($query);

// Build array
$tree = array();
while ($row = mysql_fetch_assoc($result)) {
  $tree[] = $row;
}

After that I can use following function:

// 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;

Can anyone tell me how to print above tree WITHOUT <ul> and <li>.. I want to print it using &nbsp; intended for child and sub-child... because I have to put it in <select> list and in other HTML tags

The structure I am using can be seen here: Getting a modified preorder tree traversal model (nested set) into a <ul>

Community
  • 1
  • 1
user1713941
  • 150
  • 2
  • 11

1 Answers1

0

As an urgent response (and untested), because you already have the depth of each element/node, you could use that for how many &nbsp; blocks to output for each one, and then end each line with a <br /> (unless you need to separate elements by some other means).

Try a function to output the spaces and it should be something as simple as this:

function _tab($depth) {
    $tabs = '';
    while ($depth-- > 0) $tabs .= '&nbsp;&nbsp;&nbsp;&nbsp;';
    return $tabs;
}

// bootstrap loop
$result = '';
while (!empty($tree)) {
    $currNode = array_shift($tree);
    $result .= _tab($currNode['depth']) . $currNode['title'] . '<br />';
}

print $result;
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
  • Works like charm. Thank you very very much @newfurniturey .. just one correction i needed to make was : $result **.=** _tab($currNode['depth']) . $currNode['title'] . '
    '; just added this: .=
    – user1713941 Oct 02 '12 at 14:40
  • Can you please explain the meaning of this: while ($depth-- > 0) ?? – user1713941 Oct 02 '12 at 14:47
  • @user1713941 In plain english, it means "While the depth is greater than 0, add the ` ` code to the tabs-variable, subtract `1` from the depth, and repeat.". So, if `$depth` starts at `1`, it will execute one time; if it starts at `2`, it will execute twice, etc. – newfurniturey Oct 02 '12 at 14:50
  • can you please look at this thread and reply buddy? @newfurniturey http://stackoverflow.com/questions/12706430/how-to-display-and-properly-indent-a-tree-structure-of-a-single-node .. thank you so much – user1713941 Oct 03 '12 at 10:19