The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

No worries, I managed to solve my problem by myself. It was all about altering the database table so that it contains the parent id-field (I don't need it for anything beside this). Now I just figure out if there is childgroups in group that user has chosen, if not, show products in it. Then just print all other groups with same parentid that the first one in that "tree".

That function in the article prints out all groups in tree, I need to show only active tree. I'm doing some kind of webstore and there is no need to show all groups at same time. When user clicks one group, show its childgroups and when user clicks one childgroup, show its childgroups...

Ask if I you think you can help, but didn't get the idea.

Maybe you can use the following trick:

Code:

SELECT COUNT(b.id) AS LEVEL,
a.*
FROM category a,
category b
WHERE a.lft BETWEEN b.lft AND b.rgt
GROUP BY a.id

The query above will give you the level of the category (1=root, 2=child of root, ...)

When you only want to show the direct children of the root, you can do the following:

Code:

SELECT COUNT(b.id) AS LEVEL,
a.*
FROM category a,
category b
WHERE a.lft BETWEEN b.lft AND b.rgt
GROUP BY a.id HAVING COUNT(b.id) = 2

Ok, I've got one last challenge for this article and it's code. I've converted my tree table so that the primary keys are numeric, not named with title. My fields are now: id, title, parent, type, lft, rgt.

The problem is, I want to be able to call each node directly via it's path, according to it's title:

/path/to/node/is/this/

rather than:

/0/4/14/28/12/

(preferably without heaps of queries)

The gurus here might have a better idea, but the folks on the MySQL list suggested adding another column, with the named path, so that queries on a given path (to get a specific node) are dead simple:

SELECT * FROM tree WHERE path='/path/to/node/is/this/'

Can any suggest how to modify rebuild_tree() to include the path to each node as it walks the tree and rebuilds the table?

I'm trying to design a threaded message board. I've got most of the theory ok, except I want the threads to be sorted by last post date not first post date. So I added a last post date field to the table, and each time a post is made then it updates the last post field of each parent node to the current time.

Problem is, how on earth do I sort by it? sorting by last post will break the threading. I want it to be as much as possible sorted by last post date, but within the boundaries of threading.

This article was fantastic. I got to work on it immediately. I'm far from an expert, but i think i've got it working pretty well. I will paste the functions i made for add_node and delete_node but I am having trouble making a function for move_node. If anyone has one, please paste it!

Note: I'm basically a novice when it comes to this stuff, so these functions may not be fantastic, but they seem to work for me. If anyone can see a problem with them, please make a post.

I have been playing with the modified preorder trees described here. There are several issues I am still trying to figure out:
1) The delete_node() function described here does not work for nodes that have children. If one deletes a parent node, then all its children should become children of the next node down.

2) How to write a function to move a node, and how to move a node with children so that all children are moved also.