Jump to content

Recommended Posts

Ok, im trying to code a hirearchy of employees.

There's a table with all the info i need. Let's call this table empdir_employees.

It has fields such as firstname, lastname, managerid, directoryid.

So if PersonA works for PersonB then person A's manager ID will be person B'd directory ID. So using those two table fields you can pretty much draw out a tree of who works for who and who works for them etc...

 

Sample of view...

 

CEO

    |-> VP

          |-> Director

                    |-> Manager

                              |-> The Peon.

 

So as you can see currently we have 5 levels of workers including the CEO as level 1. But 5 levels may not always be the case, and my current method of drawing out a tree requires my looping 5 times through a manually created/coded do/while loop.

 

 

small code idea of what i have. It's not actual code just more of an explanatory one. I'm using Job Titles as opposed to ID's for better understanding.

<?php
$sql = mysql_query("SELECT * FROM empdir_employees WHER manager = CEO");
//capture results & show
    do
        {
              echo name // VP
              $sql2 = mysql_query("SELECT * FROM empdir_employees WHER manager = VP");
               //capture results & show
              
              // BASICALLY RISE AND REPEAT for 5 LEVELS
        }
?>

 

So is it possible to somehow loop and loop and have the loops know when to create another loop within itself untill it reaches the last level of employee?

Link to comment
https://forums.phpfreaks.com/topic/178387-is-a-loop-like-this-possible/
Share on other sites

Yes of course. That's a typical tree structure. You can do it in a simple way by just storing a parent id on all elements (with null if it's the root). A better choice would be to use something called the "modified pre-order tree traversal algorithm" (also sometimes called "nested set"). It's more complex to implement, but it's more efficient when reading from the database. Doctrine has got an implementation of nested set that you can use.

I cant really see any examples of it working on http://www.doctrine-project.org/

 

http://www.doctrine-project.org/documentation/manual/1_1/en/hierarchical-data#nested-set

 

and is that even english >> "modified pre-order tree traversal algorithm"

 

Sure :)

 

In mathematics and computer science, a tree is a particular kind of (data) structure. Traversing a tree is the act of visiting each element in the tree, and you can do that in a number of different ways, e.g. preorder. This algorithm uses a slightly modified version of preorder traversal.

 

 

I also just remembered that I had this in my bookmarks: http://dev.mysql.com/tech-resources/articles/hierarchical-data.html

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.