473,508 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

recursive in table

190 New Member
Hi all,

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
  1.  function projectrecursive($pid)
  2.  { 
  3.     if($pid!="")
  4.     {
  5.         $qry4prj="select Project_id,Child_of,Project_name from master_info where Project_id='$pid'";
  6.     }
  7.     else
  8.     {
  9.         $qry4prj="select Project_id,Child_of,Project_name from master_info"; //Step 2
  10.     }
  11.     $Res4prj=mysql_query($qry4prj);
  12.      $cnt4prj=mysql_num_rows($Res4prj);
  13.  
  14.      if($cnt4prj>0)
  15.     {
  16.                                                                                                          while(list($prid,$childid,$tasksum)=mysql_fetch_Array($Res4prj))
  17.           {    
  18.           if($childid=="0")
  19.           {
  20.              $projectlist.='<option value='.$pr_id.'>'.$tasksum.'</option>';    //Step 3                         
  21.            }
  22.                else
  23.             {
  24.             $projectlist.=projectrecursive($prid); //Step 4 - here after getting recursive i should get out from this recursive loop but am getting continue
  25.  
  26.             }
  27.         }
  28.     }
  29.  
  30.     return $projectlist;
  31.  }
  32.  
  33. $projectcall=projectrecursive(); // Step 1
  34.  
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......
May 5 '10 #1
4 1882
Markus
6,050 Recognized Expert Expert
Is that the code you are using?
May 5 '10 #2
maheswaran
190 New Member
yes, i tried with this... Also same table and fields i used
May 5 '10 #3
Markus
6,050 Recognized Expert Expert
Please stick this at the very top of your PHP file.

Expand|Select|Wrap|Line Numbers
  1. error_reporting(-1);
May 5 '10 #4
maheswaran
190 New Member
Hi all,

Thanks, finally i got the answer. i paste the code here to may any one can use this for their recursive function

This below code will give the output of:

Project 1
------ Project 2 (Sub Project of 1)
------ Project 3 (Sub Project of 1)
------- Project 4 (Sub Project of 3)
Project 5
Project 6
------ Project 7 (Sub Project of 6)
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     $database    =    "dbname";
  3.     $dusername    =    "root";
  4.     $dpassword    =    "password";
  5.     $hostname    =    "localhost";
  6.  
  7.     mysql_connect($hostname,$dusername,$dpassword);
  8.     mysql_select_db($database);
  9.  
  10.  
  11.      $qry4prj="select Project_id,Child_of,Task_summary from project_info where Child_of='0'";
  12.      $Res4prj=mysql_query($qry4prj);
  13.      $cnt4prj=mysql_num_rows($Res4prj);
  14.  
  15.  
  16.         if($cnt4prj>0)
  17.         {
  18.              while(list($prid,$childid,$tasksum)=mysql_fetch_Array($Res4prj))
  19.             {   
  20.                      $projectlist.='<option value='.$prid.'>'.$tasksum.'</option>';
  21.  
  22.                     $projectlist.=rec($prid);
  23.                     $space="";
  24.  
  25.  
  26.             }
  27.         }
  28.          function rec($prid,$loopvalue)
  29.         {//echo "hi--".$loopvalue."<br>";
  30.             global $i,$space;
  31.             $i=$loopvalue;
  32.  
  33.             //echo "<br>1-->".
  34.             $qry4prj1="select Project_id,Child_of,Task_summary from project_info where Child_of='$prid'";
  35.              $Res4prj1=mysql_query($qry4prj1);
  36.             // echo " &nbsp;&nbsp;&nbsp; 2-->".
  37.             $cnt4prje=mysql_num_rows($Res4prj1);
  38.              if($cnt4prje>0)
  39.              {
  40.                  while(list($prid1,$childid1,$tasksum1)=mysql_fetch_Array($Res4prj1))
  41.                  {
  42.                      //echo "&nbsp;&nbsp;&nbsp; 3-->".$i."";
  43.                      $i++;
  44.                       for($j=0;$j<=$i;$j++)
  45.                      {
  46.                           //echo "&nbsp;&nbsp;&nbsp; 4 -->".$j."";
  47.                           $space.="- ";
  48.                      }
  49.                       $projectlist.='<option value='.$prid1.'>'.$space.' '.$tasksum1.'</option>';
  50.                       $loopval=$i;
  51.                       $projectlist.=rec($prid1,$loopval);
  52.                  }
  53.  
  54.              }
  55.  
  56.              return $projectlist;
  57.         }
  58.     echo "<table cellpadding='0' cellspacing='0' border='0' align='left' style='margin-left:100px;'>
  59.           <tr>
  60.                 <td> Project List    </td>
  61.           </tr>
  62.           <tr>
  63.                 <td>
  64.                 <select name='projectlistp'>
  65.                 $projectlist
  66.                 </select>
  67.                 </td>
  68.          </tr>
  69.          </table>";
  70.  
  71. ?>
  72.  
May 6 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2012
by: Magnus Lyck? | last post by:
Something really strange is happening to me (sometimes). I'm using Python 2.3.2 on NT 4.0 as well as win32all-157, adodbapi and db_row. During a recursive call to a method, it seems Python...
2
3408
by: Oxmard | last post by:
Armed with my new O'Reilly book Optimizing Oracle Performance I have been trying to get a better understanding of how Oracle works. The book makes the statement, " A database cal with dep=n + 1...
2
7014
by: Perttu Pulkkinen | last post by:
I need to find toplevel image categories and a) number of images directly in them and b) number of subcategories directly in those topcategories. In toplevel image categories "icat_parent_id IS...
6
6311
by: Tim Pascoe | last post by:
I have a database which stores information about organisms collected during sediment toxicology research. For each sample, organisms in sediment are collected and identified taxonomically (Order,...
2
5948
by: RJN | last post by:
Hi I need help in writing a recursive function. My table structure is as below. InstanceId LevelId ParentId 100 1 null 101 2 ...
5
5018
by: purushneel | last post by:
Hi, I work primarily on Oracle databases. I am trying to convert a recursive stored procedure written in Oracle to DB2. Does DB2 UDB v8.2 (Windows/AIX) supports recursive stored procedures ??...
0
1946
by: champ1979 | last post by:
I wrote an algorithm to get all the relatives of a person in a family tree. I'm basically getting all the users from the DB and am doing the recursive logic in code, so that there is only 1 call...
2
5506
by: Jim Devenish | last post by:
I have a table named StockItems with field: StockItemID QuantityInStock I am creating assemblies of stock items and have another table named StockItemAssemblies with fields:...
3
2372
by: fabiomoggi | last post by:
Hello Guys, I am developing a web application to manage Active Directory resources, and one of my tasks is to map Organizational Units hierarchy into a SQL Server database. Let's suppose that I...
3
1946
dlite922
by: dlite922 | last post by:
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...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7489
tracyyun
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...
0
5624
agi2029
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,...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.