PHP + MySQL:通过递归遍历mySQL父子组来查找总值
我有一个像这样的 mySQL 表:
文件夹
[id] [name] [parent_id]
1 fruits 0
2 orange 1
3 lemon 2
4 steak 0
项目
[id] [name] [parent_id] [hours]
1 project1 1 3
2 project2 2 4
3 project3 3 6
4 project4 4 7
基本上项目可以位于不同的文件夹中。项目的parent_id是文件夹的id。文件夹可以有子文件夹,项目可以驻留在子文件夹中。并且子文件夹的数量没有限制。
我想要得到的结果是找到文件夹及其所有子文件夹中的总小时数。所以基本上是遍历文件夹并找到该文件夹中的所有子文件夹(并继续执行直到达到最深级别),然后添加每个子文件夹中所有项目的所有小时数以获得总小时数。
将其视为任务或项目数据库,我想计算项目上花费的总时间。
有没有办法通过 mySQL 或 PHP 来做到这一点?
到目前为止,我有 mySQL 的以下信息。 http://mikehillyer.com/articles/managing-hierarchical-data-in- mysql/
我有这个 PHP 函数,但问题是我不知道如何从结果中添加总数。我不知道如何在递归函数上附加数组。无论如何,这可能是错误的,但我想我会把它扔掉。
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很困难......但我想我终于解决了这个问题。
It was tough... but I think I finally solved this.
我不确定我是否遵循您尝试对代码执行的操作,但这里是我如何组织它的框架。
I'm not sure I follow what you are trying to do with your code, but here is a skeleton of how I would organize it.