what does left_id and right_id in the table forums?

Discussion of general topics related to the new version and its place in the world. Don't discuss new features, report bugs, ask for support, et cetera. Don't use this to spam for other boards or attack those boards!
Forum rules
Discussion of general topics related to the new release and its place in the world. Don't discuss new features, report bugs, ask for support, et cetera. Don't use this to spam for other boards or attack those boards!
User avatar
ElbertF
Registered User
Posts: 583
Joined: Fri Dec 03, 2004 4:35 pm
Location: tracing..
Contact:

Re: what does left_id and right_id in the table forums?

Post by ElbertF » Wed May 28, 2008 8:17 am

Dog Cow wrote:They're for sub-forums, right? So you know what the parent forum is, and what the child forum is.

This is my assumption, anyway. :?
That's not enough information to build a tree with left and right id's, you'll need to know the relationship between all forums.

User avatar
Dog Cow
Registered User
Posts: 271
Joined: Wed May 25, 2005 2:14 pm

Re: what does left_id and right_id in the table forums?

Post by Dog Cow » Wed May 28, 2008 7:31 pm

ElbertF wrote:
Dog Cow wrote:They're for sub-forums, right? So you know what the parent forum is, and what the child forum is.

This is my assumption, anyway. :?
That's not enough information to build a tree with left and right id's, you'll need to know the relationship between all forums.
No, I believe you are mistaken. From what I know from reading the tutorial posted earlier in this topic, the entire tree can be built using just left and right ID's. They are the relationship between the forums.

User avatar
ElbertF
Registered User
Posts: 583
Joined: Fri Dec 03, 2004 4:35 pm
Location: tracing..
Contact:

Re: what does left_id and right_id in the table forums?

Post by ElbertF » Wed May 28, 2008 7:48 pm

god0fgod wrote:Can anyone please tell me how I would make the numbers for these columns?
Dog Cow wrote:They're for sub-forums, right? So you know what the parent forum is, and what the child forum is.
I believe godOfgod was asking how to calculate a left and right id for a new forum, in that case you need to know the relationship between all forums and not just the parent of that specific forum. I might be wrong.

alcaeus
Registered User
Posts: 66
Joined: Sun Oct 10, 2004 3:21 am
Location: Munich (Germany)
Contact:

Re: what does left_id and right_id in the table forums?

Post by alcaeus » Wed May 28, 2008 9:26 pm

Dog Cow wrote:No, I believe you are mistaken. From what I know from reading the tutorial posted earlier in this topic, the entire tree can be built using just left and right ID's. They are the relationship between the forums.
Not quite, you still need the information about the parent. However, using parent_id, left_id and right_id the tree can be built without problems.

Greetz
alcaeus

User avatar
Dog Cow
Registered User
Posts: 271
Joined: Wed May 25, 2005 2:14 pm

Re: what does left_id and right_id in the table forums?

Post by Dog Cow » Thu May 29, 2008 7:00 pm

First note that nowhere have I said 'rebuilt'. I read the Site Point tutorial, and I understand that the parent is needed to be known for recalculating the left and right id's.

Second note: @ ElbertF, I wasn't answering god0fgod's question, I was answering the topic starter's very first question, since reading through the replies, it seemed to me that no one had provided a clear answer.

Third note: However, to simply 'build' or perhaps I should have said 'display' the tree, only the left and right id's are needed.

Here's a link to the SitePoint article again: http://www.sitepoint.com/print/hierarch ... a-database.

alcaeus
Registered User
Posts: 66
Joined: Sun Oct 10, 2004 3:21 am
Location: Munich (Germany)
Contact:

Re: what does left_id and right_id in the table forums?

Post by alcaeus » Thu May 29, 2008 8:52 pm

Dog Cow wrote:Third note: However, to simply 'build' or perhaps I should have said 'display' the tree, only the left and right id's are needed.
Still not quite. If you want to know the indentation of an element you need to know the parent_id in order to correctly establish the parent and thus the indentation of a child. The left_id is useful to get the children in the right order (thus enabling you to get them with a single simple database query), but if you want to show a tree structure and not simply an ordered list, you need the parent.

Greetz
alcaeus

User avatar
Acyd Burn
Posts: 1838
Joined: Tue Oct 08, 2002 5:18 pm
Location: Behind You
Contact:

Re: what does left_id and right_id in the table forums?

Post by Acyd Burn » Fri May 30, 2008 7:58 am

The parent_id is not actually needed, only the parent has to be known - and this is also possible without the parent_id (the tree is build solely on left/right ids and if layed out correctly you are able to identify all parents by going through the list and spotting every direct parent from the child on).

But the parent_id is perfect for simplifying the queries a lot and gives better performance - who wants to parse a complete tree to spot the parent(s). ;)

Image

alcaeus
Registered User
Posts: 66
Joined: Sun Oct 10, 2004 3:21 am
Location: Munich (Germany)
Contact:

Re: what does left_id and right_id in the table forums?

Post by alcaeus » Fri May 30, 2008 10:55 am

Acyd Burn wrote:The parent_id is not actually needed, only the parent has to be known - and this is also possible without the parent_id (the tree is build solely on left/right ids and if layed out correctly you are able to identify all parents by going through the list and spotting every direct parent from the child on).
Could you elaborate? It somehow makes sense, but I'm not able to completely see it. I know you could just query the database and find the forum with the highest left_id that has a left_id less than the childs left_id and viceversa for the right_id, but is there a way to actually do it when looping the result set?

Greetz
alcaeus

User avatar
Acyd Burn
Posts: 1838
Joined: Tue Oct 08, 2002 5:18 pm
Location: Behind You
Contact:

Re: what does left_id and right_id in the table forums?

Post by Acyd Burn » Fri May 30, 2008 4:52 pm

alcaeus wrote:
Acyd Burn wrote:The parent_id is not actually needed, only the parent has to be known - and this is also possible without the parent_id (the tree is build solely on left/right ids and if layed out correctly you are able to identify all parents by going through the list and spotting every direct parent from the child on).
Could you elaborate? It somehow makes sense, but I'm not able to completely see it. I know you could just query the database and find the forum with the highest left_id that has a left_id less than the childs left_id and viceversa for the right_id, but is there a way to actually do it when looping the result set?
I've never tried, it has to be determined by code logic (i would bet on a recursive function :D) - therefore the "per-se standard" to use a parent id (because a child is only able to have one parent). Since childs left/right ids are always lower than it's parent you are able to query the parent by looking for the nearest left/right id pair higher than the childs.

Without having looked at the code... imagine this structure (1 forum, having 2 subforums where the first is having one subforum and the second having 2)

Code: Select all

1                                 12
|                                  |
--|                 |---------------
  2  5 ----------- 6  11
   |               |
   ---|            --|------|
     3 4            7 8     9 10

Now, imagine you want to find out the direct parent of 9/10... you would search for the nearest left/right id... you would be able to collect all parents first by doing WHERE left_id < 9 AND right_id > 10. Now, you need to "calculate" this resultset again... and so on...

Image

alcaeus
Registered User
Posts: 66
Joined: Sun Oct 10, 2004 3:21 am
Location: Munich (Germany)
Contact:

Re: what does left_id and right_id in the table forums?

Post by alcaeus » Fri May 30, 2008 5:47 pm

Ok, then I was thinking the same way you did, thanks :)

Greetz
alcaeus

Post Reply