SitePoint Sponsor

User Tag List

Page 5 of 5 FirstFirst 12345
Results 101 to 103 of 103
  1. #101
    The Omnipresent [ArcanE]'s Avatar
    Join Date
    Mar 2005
    Location
    Belgium
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found a great class here: http://dev.e-taller.net/dbtree/
    It probably already has been referenced here so sorry if its a repost.

    Now i only have a question regarding re-odering nodes. I've got everything worked out very nice: adding nodes, renaming, moving, deleting. Only reordering freaks me out a little.

    If i have a tree layout like this
    Code:
    Root
      |_ menu's
      |  |_ Day
      |  |_ Week
      |  |_ Month
      |_ contact
    It's stored in the database like this:

    Code:
    +-----------------------------------+
    | Id | Lft | Rgt | Level | Name     |
    +-----------------------------------+
    | 1  | 1   | 12  | 0     | root     |
    | 2  | 2   | 9   | 1     | menu's   |
    | 3  | 3   | 4   | 2     | day      |
    | 4  | 5   | 6   | 2     | week     |
    | 5  | 7   | 8   | 2     | month    |
    | 6  | 10  | 11  | 1     | contact  |
    +-----------------------------------+
    Now in the tree i'd like to move the "contact" node one row up in it's own level so it'll appear above the "menu's" node. Anyone can explain to me how this is done and calculated with the left and right value's?

    Thanks
    Chris

  2. #102
    SitePoint Enthusiast marcele's Avatar
    Join Date
    May 2004
    Location
    Edmonton
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jsmith
    That's a good point. It makes this a little more difficult because you need to find out if a list item has descendents, and if it does then start a nested <ul>. Here's a slightly reworked solution:

    Code:
    function display_tree($root) {
    	// retrieve the left and right value of the $root node
    	$result = mysql_query(	'SELECT L, R FROM tree '.
    									'WHERE Title="' .$root. '";');
    	$row = mysql_fetch_array($result);
    
    	// start with an empty $right stack and a $list_depth of 0
    	$right = array();
    	$list_depth = 0;
    
    	// now, retrieve all descendants of the $root node
    	$result = mysql_query(	'SELECT Title, L, R FROM tree '.
    									'WHERE L BETWEEN '.$row['L'].' AND '.
    									$row['R'].' ORDER BY L ASC;');
    
    	// start the unordered list
    	echo "<ul>";
    
    	//loop through the table
    	while ($row = mysql_fetch_array($result)) {
    
    	// only check stack if there is one
    	if (count($right)>0) {
    		// check if we should remove a node from the stack
    		while ($right[count($right)-1]<$row['R']) {
    			array_pop($right);
          }
    	}
    
    	$title = $row['Title'];
    	$current_list_depth = count($right);
    	echo "\n";
    
    	if ($current_list_depth == $list_depth) {				// List depth has not changed
    		echo "<li>$title";
    		if (($row['R'] - $row['L']) > 1) {					// We're starting a new subtree because $row has descendents
    			echo "<ul>";
    		} else {
    			echo "</li>";											// No descendents, just a entry
    		}
    	} elseif ($current_list_depth > $list_depth) {		// List depth has increased
    		echo "<li>$title";
    		if (($row['R'] - $row['L']) > 1) {
    			echo "<ul>";
    		} else {
    			echo "</li>";
    		}
    	} elseif ($current_list_depth < $list_depth) {		// List depth has decreased, therefore close some unordered lists
    		echo str_repeat ("</ul></li>\n", $list_depth - $current_list_depth);
    		echo "<li>$title";
    		if (($row['R'] - $row['L']) > 1) {
    			echo "<ul>";
    		} else {
    			echo "</li>";
    		}
    	}
    
    	// add this node to the stack
    	$list_depth = count($right);
    	$right[] = $row['R'];
    
    	}
    	echo str_repeat ("</ul></li>\n", $list_depth);		// Close all lists
    	echo "</ul>\n";												// Close the primary list
    }
    That doesn't generate valid unordered lists either .. Anyone else want to take a crack at it ?

  3. #103
    SitePoint Wizard wdmny's Avatar
    Join Date
    Jul 2000
    Location
    Here
    Posts
    1,010
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a really old thread. You should create a new thread and post more about what the problem is. I would suggest posting your output as well, because others are not going to want to build a hierarchical database just to test it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •