Jump to content

Looping While Loops?


cmcarey

Recommended Posts

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

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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! :D),

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 by cmcarey
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.