I have a mySQL table like this:
Folders
[id] [name] [parent_id]
1 fruits 0
2 orange 1
3 lemon 2
4 steak 0
Projects
[id] [name] [parent_id] [hours]
1 project1 1 3
2 project2 2 4
3 project3 3 6
4 project4 4 7
Basically projects can sit within different folders. the parent_id of projects is the id of folder. Folders can have subfolders and projects can reside within sub folders. And there is unlimited # of subfolders.
The result I would like to get is find total hours within a folder and all it's sub folders. SO basically going trough folder and finding all children folders within that folder (and keep doing it til it reach deepest level) and adding all the hours for all projects within each of those sub folders to get a total hours.
Think of it as a task or projects database and I want to calculate total hours spent on project.
Is there a way to do this via mySQL or PHP?
So far, I have the following info for mySQL. http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
And I have this PHP function but problem is I dont know how to add the total from the results. I dont know how to append an array on a recursive function. It's probably wrong anyways but I thought I'll just throw it out there.
function categoryChild($id) {
$s = "SELECT id FROM folders WHERE parent_id = $id";
$r = mysql_query($s);
$children = array();
$hours = array();
if(mysql_num_rows($r) > 0) {
# It has children, let's get them.
while($row = mysql_fetch_array($r)) {
# Add the child to the list of children, and get its subchildren
$ee['id'] = categoryChild($row['id']);
$newid = $row['id'];
$sql = "SELECT SUM(b.hours) as totalhours
FROM folders a
INNER JOIN projects b ON b.folder_id = a.id
WHERE a.id = '$newid'";
$result = mysql_query($sql);
$children['hours'] = $row['totalhours'];
}
}
$s1 = "SELECT sum(hours) as totalhours FROM projects WHERE folder_id = $id";
$r1 = mysql_query($s1);
if(mysql_num_rows($r1) > 0) {
while($row3 = mysql_fetch_array($r1)) {
$children['hours'] = $row3['totalhours'];
}
}
$data = $data['hours'];
return $data;
}