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 -
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
-
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......
4 1888 Markus 6,050
Recognized Expert Expert
Is that the code you are using?
yes, i tried with this... Also same table and fields i used
Markus 6,050
Recognized Expert Expert
Please stick this at the very top of your PHP file.
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) -
<?php
-
$database = "dbname";
-
$dusername = "root";
-
$dpassword = "password";
-
$hostname = "localhost";
-
-
mysql_connect($hostname,$dusername,$dpassword);
-
mysql_select_db($database);
-
-
-
$qry4prj="select Project_id,Child_of,Task_summary from project_info where Child_of='0'";
-
$Res4prj=mysql_query($qry4prj);
-
$cnt4prj=mysql_num_rows($Res4prj);
-
-
-
if($cnt4prj>0)
-
{
-
while(list($prid,$childid,$tasksum)=mysql_fetch_Array($Res4prj))
-
{
-
$projectlist.='<option value='.$prid.'>'.$tasksum.'</option>';
-
-
$projectlist.=rec($prid);
-
$space="";
-
-
-
}
-
}
-
function rec($prid,$loopvalue)
-
{//echo "hi--".$loopvalue."<br>";
-
global $i,$space;
-
$i=$loopvalue;
-
-
//echo "<br>1-->".
-
$qry4prj1="select Project_id,Child_of,Task_summary from project_info where Child_of='$prid'";
-
$Res4prj1=mysql_query($qry4prj1);
-
// echo " 2-->".
-
$cnt4prje=mysql_num_rows($Res4prj1);
-
if($cnt4prje>0)
-
{
-
while(list($prid1,$childid1,$tasksum1)=mysql_fetch_Array($Res4prj1))
-
{
-
//echo " 3-->".$i."";
-
$i++;
-
for($j=0;$j<=$i;$j++)
-
{
-
//echo " 4 -->".$j."";
-
$space.="- ";
-
}
-
$projectlist.='<option value='.$prid1.'>'.$space.' '.$tasksum1.'</option>';
-
$loopval=$i;
-
$projectlist.=rec($prid1,$loopval);
-
}
-
-
}
-
-
return $projectlist;
-
}
-
echo "<table cellpadding='0' cellspacing='0' border='0' align='left' style='margin-left:100px;'>
-
<tr>
-
<td> Project List </td>
-
</tr>
-
<tr>
-
<td>
-
<select name='projectlistp'>
-
$projectlist
-
</select>
-
</td>
-
</tr>
-
</table>";
-
-
?>
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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 ...
|
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)
|
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
| |
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...
|
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...
|
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
|
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
|
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
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |