cmcarey Posted October 31, 2012 Share Posted October 31, 2012 Hey guys, I'm completely confused. Situation: I have a chain of command in my business. Every person in this business has a mysql record that lists every one of their employees. I can pull all of the employees from one person into a single array - already done. Let's call that $people1[]; I then need to pull out all of the employees for every person in the $people1[] array. Not too hard, I can just use while() and loop those into another array. But I can't do this for over 200 employees working in a staggered heir-achy system. So essentially I need an efficient way of doing while within while within while, up to 200 times. My end target: function doesWorkForMe ($subjectName); What this needs to achieve is either return true or false if that person is inside their chain of command. It doesn't have to be directly - if they work for someone who works for someone who works for ME, it needs to return true. But anyone above ME returns a false. Can the brains of this community please help me out? Cheers, CM Quote Link to comment https://forums.phpfreaks.com/topic/270124-looping-while-loops/ Share on other sites More sharing options...
kicken Posted October 31, 2012 Share Posted October 31, 2012 Assuming you have a parent-child relationship going in your database at the moment, you could select all the data and build a tree out of it using some references and a loop. It's not the most efficient method but should be fine for anything less than a thousand entries or so. The big thing to avoid is doing queries within your loop. By selecting everything in a single query you save time gathering data and just focus on processing it. This article describes how to build the tree out of the list. http://jacobsantos.com/2009/general/dynamic-tree-building-in-php/ A better solution, if you're able to re-structure the database would be to set it up as a nested set system, which is described in this article: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ With the nested set system finding all of the parent's of a particular item is a simple SELECT query. Quote Link to comment https://forums.phpfreaks.com/topic/270124-looping-while-loops/#findComment-1389074 Share on other sites More sharing options...
Psycho Posted October 31, 2012 Share Posted October 31, 2012 Are you running queries in these loops? If so, STOP. You should run ONE query to get all the data you need then handle the logic in the processing code. It would help to know the exact structure of the database. You state that Every person in this business has a mysql record that lists every one of their employees Are you saying that there is a single field with a delimited list of IDs? If so, that's going to be difficult to impossible to achieve what you want. Hopefully, the records of subordinate records are determined via an associative table or the user table has the ID of their supervisor. Quote Link to comment https://forums.phpfreaks.com/topic/270124-looping-while-loops/#findComment-1389076 Share on other sites More sharing options...
cmcarey Posted October 31, 2012 Author Share Posted October 31, 2012 I was completely wrong - turns out its activedirectory and not mysql. Sorry. Any other ways around? Cheers, CM Quote Link to comment https://forums.phpfreaks.com/topic/270124-looping-while-loops/#findComment-1389077 Share on other sites More sharing options...
Barand Posted October 31, 2012 Share Posted October 31, 2012 (edited) Basically, we need to see how your data is structured. EDIT Just saw the reference to active directory. That doesn't make it impossible, just more difficult. Get hold of class called "ADLDAP". That makes working with AD a whole lot easier. Edited October 31, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/270124-looping-while-loops/#findComment-1389079 Share on other sites More sharing options...
cmcarey Posted October 31, 2012 Author Share Posted October 31, 2012 (edited) I can call a function getUnder($user) that results in every name of every person that is under the chain of the $users command. I need to loop through everyone who is under $users command, and then everyone under their command again, a lot of times to see everyone under the original $users command. Cheers (Awesomely speedy responses! ), CM Edit: Even though I'm working with AD, I would prefer if the code was expandable so that it works even in other situations, so pure php is what I really need. Edited October 31, 2012 by cmcarey Quote Link to comment https://forums.phpfreaks.com/topic/270124-looping-while-loops/#findComment-1389081 Share on other sites More sharing options...
Barand Posted October 31, 2012 Share Posted October 31, 2012 try this approach <?php /*************************************** * Construct an array of staff * where the key is each Mgr and the * values are arrays of staff that work * for each manager. ***************************************/ $staff = array ( 'emp1' => array ( 'emp2', 'emp3', 'emp4' ), 'emp2' => array ( 'emp5', 'emp6' ), 'emp3' => array ( 'emp7', 'emp8', ), 'emp6' => array ( 'emp9', 'emp10', 'emp11', 'emp12' ) ); /************************************** * Now you need a recursive function to * traverse the array and output your * staff hierarchy ***************************************/ function listStaff (&$staff,$mgr, $level=0) { $indent = str_repeat(' ', 5*$level); echo "$indent$mgr<br />"; if (isset($staff[$mgr])) { foreach ($staff[$mgr] as $emp) { listStaff($staff, $emp, $level+1); } } } /********************** * print the list ***********************/ listStaff ($staff, 'emp1'); //where emp1 is CEO ?> RESULTS emp1 emp2 emp5 emp6 emp9 emp10 emp11 emp12 emp3 emp7 emp8 emp4 Quote Link to comment https://forums.phpfreaks.com/topic/270124-looping-while-loops/#findComment-1389099 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.