Best method for dealing with large level hierarchies

Associate
Joined
4 Mar 2007
Posts
315
Location
United Kingdom
So, i'm building a system for educational purposes that will have several core fundamentals, most of the system is in place but there's one part I can't quite decide on when it comes to efficiency.

The system's idea is to be able to load modules and load augments (modules that fit into modules) in a literal file sense.
For example,
There is a directory called modules. You can in essence look at a module like an addon. However, I have one class that auto instantiates new modules into the system, the purpose is that each module is going to be a unique calculation that the user can either engage into an equation or disengage.

The layout is as such:

Modules/
Augments/
System/

In modules you may have something like this:

Code:
Modules/
   EquationA.module.php
   EquationB.module.php
Augments/
   EquationA.augment.php
   EquationB.augment.php
System/
   main.class.php

This all works fine, however the part i'm struggling with is a hierarchy part of the system....

I need to be able to call an element which may be required in EquationA.module.php, this element has data in a database. However it may be a child of some other element. The database looks like this:

Code:
ID  Title   Parent   Magnitude
1    A       NULL        150
2    A-1       1            5
3    A-2       1           24
4    A-1-1    3           12

So let's say the user has an equation in EquationA.module.php which takes
A-2 and multiplies it by B-1-1, that would be rather simple, however. I wish to display these Titles in a drop down fashion like so:

Code:
A
  A-1
      A-1-1
  A-2
...

Is there anyway or any script that does this for me.
The modules and augmentations work perfectly and are very efficient for my first time at messing around with this depth of php, but this part has got me stumped.
 
Have a look around google for PHP and SQL that deals with filesystems. I've used T-SQL and ASP (at the time) to create and display a recordset that represented a file/folder structure similar to windows explorer, which sounds vaguely similar to what you're trying to achieve.

I seem to remember there being a lot more information about displaying filesystem hierarchies than anything else, so you shouldn't have any issue finding some code.

The solution was made up of a T-SQL stored procedure which would pull out a segment of the tree from any given node. So if nothing was passed, you got a recordset of the whole tree, and if you passed a a node (say A-1 in your example) you would get a recordset containing the tree as if that node was the topmost node.

The ASP I used simply used 2 recordsets and the stored procedure to display the heirarchy graphically.

Hope this helps.
 
Have a look around google for PHP and SQL that deals with filesystems. I've used T-SQL and ASP (at the time) to create and display a recordset that represented a file/folder structure similar to windows explorer, which sounds vaguely similar to what you're trying to achieve.

I seem to remember there being a lot more information about displaying filesystem hierarchies than anything else, so you shouldn't have any issue finding some code.

The solution was made up of a T-SQL stored procedure which would pull out a segment of the tree from any given node. So if nothing was passed, you got a recordset of the whole tree, and if you passed a a node (say A-1 in your example) you would get a recordset containing the tree as if that node was the topmost node.

The ASP I used simply used 2 recordsets and the stored procedure to display the heirarchy graphically.

Hope this helps.


Brilliant, had no clue that such a thing existed, did a bit of research and implementation, can now engage and disengage sections pretty efficiently. Got all the data down to a rather simple array in the end which will increase dynamically while keeping the hierarchies intact e.g.

Code:
Array
(
    [group] => Array
        (
            [@attributes] => Array
                (
                    [name] => A
                )

            [item] => Array
                (
                    [0] => Array
                        (
                            [@attributes] => Array
                                (
                                    [name] => a
                                )

                        )

                    [1] => Array
                        (
                            [@attributes] => Array
                                (
                                    [name] => b
                                )

                        )

                )

        )

)

This actually is then output into an XML file as a back up like so:

Code:
<relationships>

    <group name="A">
        <item name="a"></item>
        <item name="b"></item>
    </group>
    
</relationships>

Thanks again for the help!
 
Back
Top Bottom