Show users in tree structure?

I have a database table named users

There are two columns in this table (which are important)

uuid and parentUuid

Here are the rules for the table:

  • If the user invites another user, the invited user column parentUuidis equal to the uuidinvitation.
  • If the user was not invited, their column parentUuid:null
  • There can be endless user levels.

What I want to do is create a function function counter($levels, $uuid){}

When the counter is called (say $ levels = 3)

I want the function to return an array that looks like

array(0 => 200, 1 => 600, 2 => 1800);

So, the main idea is that I want it to count, for $levelslevels, how many users are in the tree under the user.

What is the best way to do this?

+3
4

, "level", . , , + 1.

, , , , , , , , . , -, , .

+3

SAP , :

-, , UnID, UnID. UnID .

Then, when you add members to your hierarchy table, you should store them in this format:

NodeID  
    Note: 0 is always the root nodeID. Any other entry is the UnId of the new entry. It can never be null

ParentID 
    Note: this is the UnID of the parent that you want to attribute to the new entry. It can never be null

ChildId 
    Note: this can be null. It is updated only when this New entry gets to be a parent. 

NextId 
    Note: this is the important one. It determines which child is next in the sequence of children below the parent. The last one in the sequence is always null

Level
    Note: this ensures that a UnID cannot be the parent at more than one level.

You will need some programming logic to smooth out the attempts to create the circular links mentioned here in the comment, but it does mean that when the referral happens, you have to update three records in your hierarchy table: new record, parent and last parent of the parent ( with the following IDID) to ensure the correct pyramid.

0
source

All Articles