I need to get a list of employees out of a database table.
I need to end up with an array of ids (primary keys) such as
Array(03,9,2,5,1)
The employee table has a self reference so that each record has a parentID and a level. For example -
id_____parentID____Level________Name
-
12_____15__________1_____________John
-
15_____-1__________0_____________Harry
-
19_____15__________1_____________Smith
-
20_____19__________2_____________Jane
-
In this example, Harry is the top manager. He has two managers under him, John and Smith, further more Smith has Jane under him. (get your mind out of the gutter!)
Instead of the recursive function calling the database each time to get an id, I'll do one call and put the entire employee table in an array of assoc array(id, parentID) since there's only several records.
[PHP]
Array(
[0] = Array("
["id"] = 12,
["parentID"] = 15
)
[1] = Array (......and so on, you get the point)
[/PHP]
The recursive function will be given the primary id of the employee I need to build for. The returned array list will contain this id along with every employee under him, going down the tree as needed. I'll do one more thing, i'll even give you the employee array (shown above) as the second parameter like so:
function Recursive($_SESSION['employeeID'], $employeeArray)
{
// do magic here
}
does anybody get the idea? I never really liked recursive functions, but If I build it, i'll post it here.
thanks,
Dan
3 1942
You would want to have a look at Tree structures on the internet..
You should easily be able to find recursive algorithms to traverse a tree....
Basically, there are two options....
Pre-Traversal (where you use the current node BEFORE it's childeren (this is the one you want, I think)
Post-Traversal (where you deal with the curtrent node AFTER it's childeren)
(There is also a third for Binary trees, but I doubt this structure can be limited to a binar y tree...
Basically, in non-php terms, what you want to do is: -
-
Recurse(EmployeeId, ChildrenArray)
-
Print out EmployeeID (and anything else you want to print out)
-
foreach Child in ChildrenArray do
-
Recurse(Child, Child->ChildrenArray);
-
endloop
-
endfunction
-
Did it, please let me know if there's any mistakes you see.
[PHP]
$employees= array(
array("id" => 1, "parentID" => 2),
array("id" => 2, "parentID" => 3),
array("id" => 3, "parentID" => -1),
array("id" => 4, "parentID" => -1)
);
function buildEmpList($runningList,$employees, $employeeID)
{
array_push($runningList, $employeeID);
foreach ($employees AS $emp)
{
if($emp['parentID'] == $employeeID)
{
$runningList = $runningList + buildEmpList($runningList, $employees, $emp['id']);
}
}
return $runningList;
}
$result = array();
print_r(buildEmpList($result, $employees,3));
//outputs:
Array
(
[0] => 3
[1] => 2
[2] => 1
)
[/PHP]
This drove me nuts for a little while.
-Dan
You would want to have a look at Tree structures on the internet..
You should easily be able to find recursive algorithms to traverse a tree....
Basically, there are two options....
Pre-Traversal (where you use the current node BEFORE it's childeren (this is the one you want, I think)
Post-Traversal (where you deal with the curtrent node AFTER it's childeren)
(There is also a third for Binary trees, but I doubt this structure can be limited to a binar y tree...
Basically, in non-php terms, what you want to do is: -
-
Recurse(EmployeeId, ChildrenArray)
-
Print out EmployeeID (and anything else you want to print out)
-
foreach Child in ChildrenArray do
-
Recurse(Child, Child->ChildrenArray);
-
endloop
-
endfunction
-
Thanks, yes I'm familiar with BTrees, and Sorting Algorithms. breadth-first searches (I paid attention in algorithm class)
But I barely got a C.
Anyways, I wouldn't care what order they are in, these will end up right back in a MySQL query IN() where-clause, wouldn't matter.
It's been a while since I wrote a recursive function.
Dan
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Tim Pascoe |
last post by:
I am writing a function which I hope to use as a column value in a
select query. The function recursively walks a taxonomic heirarchy,
extracting the name for an organism at the taxonomic level...
|
by: |
last post by:
Hello All, this is my first post.
OK - The goal is to display the following (note: substitute 1' ' for 2'*')
by using 3 recursive functions.
0123454321001234543210
**012343210012343210**...
|
by: |
last post by:
OK:
Purpose: Using user's input and 3 recursive functions, construct an hour
glass figure. Main can only have user input, loops and function calls.
Recursive function 1 takes input and displays...
|
by: ali |
last post by:
Hi,
I'm trying to work on a recursive function that will give me root
valuefor a given number.
What i mean by root value is, if given 13, the answer is 1+3 = 4. If
given 65, the answer is 2,...
|
by: Richard A. DeVenezia |
last post by:
Can a function write another function that has a specific number of
nested loops and then run it?
i.e.
function maker (N) {
// does stuff that creates function doer()
// invoke doer
doer()
}
|
by: pauldepstein |
last post by:
I am writing code to price some financial options using recursive
functions.
It compiles but I get a runtime error which is caused by a memory
violation.
The problem is that the code is much...
|
by: join |
last post by:
I went to solve a problem related
to Palindrome
int isPalindrome(char *c);
returns back 1 if the string is a palindrome and returns back zero if
it is not a palindrome.
but without using...
|
by: Michael L |
last post by:
Hi Guys(I apologize for the lengty post - Im trying to explain it as
best i can)
I've been cracking my head on this one for the past 24+ hours and i
have tried creating the function in ten...
|
by: Amali |
last post by:
I'm newdie in c programming. this is my first project in programming.
I have to write a program for a airline reservation. this is what i
have done yet. but when it runs it shows the number of...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |