iPixel Posted October 20, 2009 Share Posted October 20, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/178387-is-a-loop-like-this-possible/ Share on other sites More sharing options...
Daniel0 Posted October 20, 2009 Share Posted October 20, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/178387-is-a-loop-like-this-possible/#findComment-940725 Share on other sites More sharing options...
iPixel Posted October 20, 2009 Author Share Posted October 20, 2009 I cant really see any examples of it working on http://www.doctrine-project.org/ and is that even english >> "modified pre-order tree traversal algorithm" Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/178387-is-a-loop-like-this-possible/#findComment-940750 Share on other sites More sharing options...
Daniel0 Posted October 20, 2009 Share Posted October 20, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/178387-is-a-loop-like-this-possible/#findComment-940755 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.