Recursive function FAIL.
Posted: Sun Jan 16, 2011 12:16 pm
Okay, so I am building a task management system, and I wanted to give the tasks an unlimited depth of subtasks, such that each task could have as many subtasks as necessary, and each of those subtasks could have subtasks, etc.
So I wrote a recursive function for retrieving said tasks from the database.
Here's the deal: it fails on the first recurse. Now if I call the stored proc with the params that are being passed in on the first recurse, I get the data i expect. I think the deal is that the database connection is not free.
What can I do to fix this?
Thanks,
-B
So I wrote a recursive function for retrieving said tasks from the database.
Code: Select all
private function GetTasks($userId, $parent = 0){
global $database;
$query = "CALL GetTasks($userId, $parent);";
$result = mysql_query($query);
$tasklist = array();
if($result != false){
while($row = mysql_fetch_object($result)){
$task = new Task();
$task->id = $row->id;
$task->text = $row->text;
$task->complete = $row->complete;
$task->parent_id = $row->parent;
$task->children = $this->GetTasks($userId, $task->id);
array_push($tasklist,$task);
}
}
return $tasklist;
}
What can I do to fix this?
Thanks,
-B