I have problem in getting data from database using recursive function...
I have the table called "master_info"
project id | Child_of | Project_name
=====================================
1 | 0 | Project 1
2 | 0 | Project 2
3 | 1 | Sub project of 1
I want project list in drop down list like
Project 1
- - Sub project of 1
Project 2
Project 3
For this i did some recursive function
Expand|Select|Wrap|Line Numbers
- function projectrecursive($pid)
- {
- if($pid!="")
- {
- $qry4prj="select Project_id,Child_of,Project_name from master_info where Project_id='$pid'";
- }
- else
- {
- $qry4prj="select Project_id,Child_of,Project_name from master_info"; //Step 2
- }
- $Res4prj=mysql_query($qry4prj);
- $cnt4prj=mysql_num_rows($Res4prj);
- if($cnt4prj>0)
- {
- while(list($prid,$childid,$tasksum)=mysql_fetch_Array($Res4prj))
- {
- if($childid=="0")
- {
- $projectlist.='<option value='.$pr_id.'>'.$tasksum.'</option>'; //Step 3
- }
- else
- {
- $projectlist.=projectrecursive($prid); //Step 4 - here after getting recursive i should get out from this recursive loop but am getting continue
- }
- }
- }
- return $projectlist;
- }
- $projectcall=projectrecursive(); // Step 1
I called recursive function with out any project id.
Step 2:
Check the condition , if project id is null then it will get the data with out any condition. Its for getting the project id and name which they have not child_of i.e who are not any child project for another projects
Step 3
Listing the projects which they not child for other projects [i.e child_of = 0]
Step 4
if any projects is child of to any one of project [i.e child_of = have sum value] then i called recursive function to get the child & parent project name
In this step i getting project id as 3 also child_of is 1 , and program should go to out of loop but problem is i got condition true [i.e child_of = have sum value] so i getting value 3 as projekct id and 1 as child_of value and its getting continue loop....
How i solve it......