473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Updating a tree structure

TheServant
1,168 Expert 1GB
Hi Guys,
I am trying to get some good logic down on paper, but before I waste my time with this path of thinking I was wondering if you can tell me any majory drawbacks to the logic.

Imagine a tree structure,
A / B C
B / D E
D / F G
E / H I

_____________A________
________B_________C___
____D_______E_________
__F___G___H___I_______


Everyone under A will be in Group A. Now if I move B from being under A to being under noone, he (and all those under him) will be in group B. I am trying to automate this as those under B will vary in number and structure. Each person has a superior (that is how the database works):

Logic:
[PHP]function change_superior(person, new_group) {
mysql_query("UPDATE table1
SET group = $new_group
WHERE name = $person");

foreach( mysql_query("SELECT * FROM table1 WHERE superior=$person") as $under ) {
change_superior($under,$new_group);
}
}
[/PHP]

Quite shakey on foreach loops so my syntax is probably way off, but let me know what you think.
Jul 16 '08 #1
8 1696
pbmods
5,821 Expert 4TB
Heya, TheServant.

mysql_query() returns a resource that you run through mysql_fetch_object() to retrieve your rows (http://php.net/mysql_fetch_object):

Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query('...');
  2. while( $row = mysql_fetch_object($result) )
  3. {
  4.   process_row($row);
  5. }
  6.  
Jul 16 '08 #2
Atli
5,058 Expert 4TB
You don't really need the "group" column there, do you?

I mean, if I were to map a path to person "F", it would look like so:
"A->B->D->F".

From that we can deduce that F belongs to A, as A is it's the top-level parent.
If you were to change B's parent to null, F's top-level parent would become B, which automatically achieves your goal, doesn't it?

In practice, you would only have to assign each person a "parent", and to find which group it belongs to, you would just have to climb to the top-level parent.

Somewhat like:
Expand|Select|Wrap|Line Numbers
  1. function getTopLevelParent($person) {
  2.   $sql = "SELECT parent FROM tbl WHERE person = '$person'";
  3.   $result  = mysql_query($sql);
  4.   $row = mysql_fetch_row($result);
  5.  
  6.   if($row['parent'] != null)
  7.     return getTopLevelParent($row['parent']);
  8.   else
  9.     return $row['parent'];
  10. }
  11.  
Jul 16 '08 #3
TheServant
1,168 Expert 1GB
Heya, TheServant.

mysql_query() returns a resource that you run through mysql_fetch_object() to retrieve your rows (http://php.net/mysql_fetch_object):

Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query('...');
  2. while( $row = mysql_fetch_object($result) )
  3. {
  4.   process_row($row);
  5. }
  6.  
Cheers, knew it was something like that but don't have my old code to go through where I have used it before. What about the actual function? Is it calling itself ok? No glaring problems you can see?

And with teh loop, should it be:
$result = mysql_query('...');
foreach( $row = mysql_fetch_object($result) )
?

Thanks for your reply.
Jul 16 '08 #4
TheServant
1,168 Expert 1GB
You don't really need the "group" column there, do you?

I mean, if I were to map a path to person "F", it would look like so:
"A->B->D->F".

From that we can deduce that F belongs to A, as A is it's the top-level parent.
If you were to change B's parent to null, F's top-level parent would become B, which automatically achieves your goal, doesn't it?

In practice, you would only have to assign each person a "parent", and to find which group it belongs to, you would just have to climb to the top-level parent.

Somewhat like:
Expand|Select|Wrap|Line Numbers
  1. function getTopLevelParent($person) {
  2.   $sql = "SELECT parent FROM tbl WHERE person = '$person'";
  3.   $result  = mysql_query($sql);
  4.   $row = mysql_fetch_row($result);
  5.  
  6.   if($row['parent'] != null)
  7.     return getTopLevelParent($row['parent']);
  8.   else
  9.     return $row['parent'];
  10. }
  11.  
Good point. I will remember that. The only thing is, the top-level parent will be a group name and not a person's name, so I will need a group column, and I think it will need updating. Unless everytime I want to see a user's group it searches to find the top level parent and puts his group there?

Correct me if I'm wrong but wouldn't it be quicker to update every person in the tree if someone changes (shouldn't happen that often) so when I recall a user it is in their own row? Hope I made sense.
Jul 16 '08 #5
Atli
5,058 Expert 4TB
Good point. I will remember that. The only thing is, the top-level parent will be a group name and not a person's name, so I will need a group column, and I think it will need updating. Unless everytime I want to see a user's group it searches to find the top level parent and puts his group there?

Correct me if I'm wrong but wouldn't it be quicker to update every person in the tree if someone changes (shouldn't happen that often) so when I recall a user it is in their own row? Hope I made sense.
In that case your original concept isn't a bad one.
Adding a group name to each person would be an awful wast of space tho.

You should consider creating a different table for the groups, where you list all group names. Then you can simply link every person to it's group using the ID of the group as a foreign key in the person table.

If for some reason you change the group for the leader, and you want all his minions to follow him, the you would have to change the group ID for them all which would require a recursive update function... somewhat like you posted in your original post.

Personally, I wouldn't use this sort of hierarchy to control user groups.
I would create some sort of a role system, somewhat like the "Admin->Mod->Member" concept of a forum.
Then each person could be linked to multiple groups and even given different roles for each group.
Jul 16 '08 #6
TheServant
1,168 Expert 1GB
In that case your original concept isn't a bad one.
Adding a group name to each person would be an awful wast of space tho.

You should consider creating a different table for the groups, where you list all group names. Then you can simply link every person to it's group using the ID of the group as a foreign key in the person table.

If for some reason you change the group for the leader, and you want all his minions to follow him, the you would have to change the group ID for them all which would require a recursive update function... somewhat like you posted in your original post.

Personally, I wouldn't use this sort of hierarchy to control user groups.
I would create some sort of a role system, somewhat like the "Admin->Mod->Member" concept of a forum.
Then each person could be linked to multiple groups and even given different roles for each group.
Sorry, I was simplifying the problem, but yes, it will be a group ID column, as I will have a group table with it's own group variables. And for interest sake, its going to be so that users can create their own groups and can't join any others. Also the hierarchy wil be infinite so the tree width and length has no limit.

Along with foreach loops, I have limited experience using forign keys. Any good tutes? Is it done when creating the table? Thanks for your help, always know I will find answers here!
Jul 16 '08 #7
pbmods
5,821 Expert 4TB
And with teh loop, should it be:
$result = mysql_query('...');
foreach( $row = mysql_fetch_object($result) )
?
It should be while( $row = mysql_fetch_object($result) ). foreach is only used to loop through an array.
Jul 20 '08 #8
TheServant
1,168 Expert 1GB
It should be while( $row = mysql_fetch_object($result) ). foreach is only used to loop through an array.
Point taken. Cheers mate.
Jul 22 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Steve Johnson | last post by:
Been banging my head on this for two days now. Hope someone can help! My test program below is in the form of a single JSP, with a Node class build in. (All the coded needed to run is below.) ...
1
by: googleo | last post by:
Hi, in my application I want to handle and store data in a hierarchic data structure. For example: persons who manage houses; houses have various numbers of floors; floors have various numbers...
3
by: Robin Tucker | last post by:
Hi, I'm in the process of implementing a multi-user system containing an adjacency list (tree structure). I'm using a TIMESTAMP field on each record in the adjacency list in order to tell when...
1
by: Srihari | last post by:
I'm trying to develop a tree structure using javascript. The node values of the tree are generating from a mysql table depending on login. The tree structure contains 3 sub levels. I developed...
4
by: Stephan Tobies | last post by:
Hi everyone, I am looking for a good data structure that could be used to represent families of trees with shared sub-trees and copy-on-write semantics. On a very abstract level, I would like...
1
by: David Hirschfield | last post by:
I've written a tree-like data structure that stores arbitrary python objects. The objective was for the tree structure to allow any number of children per node, and any number of root nodes...and...
5
by: hankypan1 | last post by:
Hi All, I need a tree data structure for my application. It is the non -cyclic simple tree where i can have any number of children node and each child can recursively become a sub tree like a...
8
by: =?ISO-8859-1?Q?m=E9choui?= | last post by:
Problem: - You have tree structure (XML-like) that you don't want to create 100% in memory, because it just takes too long (for instance, you need a http request to request the information from...
0
by: mac | last post by:
I found that with memory allocating techniques used nowadays (addresses alignment, eg. on 32bit machines) one can detect loops in a tree structure very fast, without using extra memory. This is due...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.