views:

242

answers:

1

Hi, I've been working on a site that uses binary mlm system.

Illustration here

So I have a two tables in database, users anad relationships. There is ID and personal data columns in users. Relationships has 4 columns: ID, parentID, childID, pos. Where pos is either left or right.

I have succesfully written a function that recursively lists all children of given pid (parentID). However I need to sort it in levels (for display and calculation purposes).

I have an array of children of user ID = 1:

Array
(
    [0] => Array
        (
            [id] => 2
            [parentID] => 1
            [pos] => l
        )

    [1] => Array
        (
            [id] => 4
            [parentID] => 2
            [pos] => l
        )

    [2] => Array
        (
            [id] => 8
            [parentID] => 4
            [pos] => l
        )

    [3] => Array
        (
            [id] => 5
            [parentID] => 2
            [pos] => p
        )

    [4] => Array
        (
            [id] => 3
            [parentID] => 1
            [pos] => p
        )

    [5] => Array
        (
            [id] => 6
            [parentID] => 3
            [pos] => l
        )

    [6] => Array
        (
            [id] => 7
            [parentID] => 3
            [pos] => p
        )

)

Now I have function named get_levels that returns an multidimensional array that should look like this:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [parentID] => 1
                    [pos] => l
                )

            [1] => Array
                (
                    [id] => 3
                    [parentID] => 1
                    [pos] => p
                )

        )
   [1] => Array
        (
            [0] => Array
                (
                    [id] => 4
                    [parentID] => 2
                    [pos] => l
                )

            [1] => Array
                (
                    [id] => 5
                    [parentID] => 2
                    [pos] => p
                )
            [2] => Array
                (
                    [id] => 6
                    [parentID] => 3
                    [pos] => l
                )

            [3] => Array
                (
                    [id] => 7
                    [parentID] => 3
                    [pos] => p
                )

        )
  ETC.

)

Here's the function:

 function get_levels($pid,$level, $level_id){
       $children = children_array($pid,1);
       if (sizeof($children) > 0):
          foreach ($children as $child):
             if ($child["parentID"] == $pid):


          get_levels($child["id"], $level, $level_id+1);
          $level[$level_id][] = $child;   


        endif; 


         endforeach;
      endif;
      return $level;
 }

function children_array($pid, $depth) returns the children ... for $depth = 1 it returns immediate children (0 or 1 or 2), for $depth = 0 it returns all children

Can anyone help me with this function? I think the function works, however I don't know how to recursively use and add to array.

A: 

Looks like you're using a data structure within the wrong context. It's a binary tree, yet it's represented into a multilevel array which in short doesn't define its boundaries and rules of use.

When using the tree, I would use something like a Node class that has two children, left and right. Iterating through the tree would be piece of cake, inserting/deleting/editing into it is easily done depending on which set of rules you want to follow. When storing the tree, I would use some kind of Ahnentafel list which can easily be done in a relational database.

I would in no way mix both iteration and storage processes because if I change the rules of storage, I might also have to change the rules of iteration and vice versa.

JP
I've found this binary tree class: http://www.phpclasses.org/browse/file/11713.htmlWhat should be the best way to store data in database? ID|parentID|leftChildID|rightChildID ?
Smaug
For data I would only refer to the parent and may the position if there is no other way to figure it out. The node that has no parent is the root.
JP