473,664 Members | 2,972 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_inf o"

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 1888
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
2015
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 messes up its variable bindings once in a while. Suddenly, one of several local variables gets rebound to the object it was bound to one step up in the recursion. The relevant part of the code looks like this with print statements
2
3423
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 is the recursive child of the first subsequent dep=n database call listed in the SQL data stream. The book gives a few examples, and in trying it out it seemed to work until I tried the following SQL. My question are why does this not keep with...
2
7022
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 NULL". Below is also current erraneous draft but it has ambigous filed name problem. CREATE TABLE icategories ( icat_id int(11) NOT NULL auto_increment, icat_parent_id int(11) default NULL, icat_name char(100) default NULL, PRIMARY KEY ...
6
6319
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, Family, Genus, Species). Taxonomy lookup information in the database is stored in a recursive table in the form: TSN (taxa serial number) Rank (Order, Family, Genus, Species)
2
5985
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 100 102 3 101 103 4 102
5
5026
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 ?? After some research, I found out that to call recursively in DB2, the stored procedure should be CALLed using dynamic SQL. I am not sure whether it is the right way. Am I missing something ?? Please let me know...
0
1954
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 made to the DB. However, I am trying to do the same thing within a stored procedure in SQL using recursive CTEs (I think the performance might be better) but I'm finding it really tough to craft the CTE. I would really appreciate if someone could...
2
5527
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: StockItemAssemblyID AssemblyID StockItemID Quantity
3
2382
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 have the following OU hierarchy in my Active Direcoty: 1. NewYork 1.1 HR_Department 1.1.1 Computers 1.1.2 Users 1.2 SALES_Department 1.1.1 Computers
3
1955
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 each record has a parentID and a level. For example id_____parentID____Level________Name
0
8437
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8861
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8636
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7375
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.