recursive function problem

having a problem with my function

Page 1 of 1

6 Replies - 4134 Views - Last Post: 18 August 2005 - 03:29 PM Rate Topic: -----

#1 jasdulan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-August 05

recursive function problem

Posted 17 August 2005 - 06:24 PM

This is a recursive function problem.

Ok, My table data is as follows
*table data isn't formatting properly, you can see it at;
http://www.hksinc.co...in/all_data.php

___________________________________________

page_id page_title page_parent_id page_order
1 Home 0 1
2 Architectural Visualization 0 2
30 Web Design 0 3
32 HKS, Inc. 0 4
31 Contact Us 1 1
37 Tridot 2
29 Rendering 2 1
3 Animation 2 2
33 Staff 2 5
38 Design Sub 1 22
41 Production Sub 1 27
22 Design 30 1
27 Production 30 2
39 Design Sub 2 (link) 38
42 Design Sub 2b 38
40 Design Sub 3 39
___________________________________________


My goal is for my page to display the following and have infinite parent-child levels.

___________________________________________
Home
Contact US

Architectural Visualization
Tridot
Rendering
Animation
Staff

Web Design
Design
Design Sub 1
Design Sub 2 (link)
Design Sub 3
Design Sub 2b
Production
Production Sub 1

HKS, Inc.
___________________________________________

My function is as follows

___________________________________________
<?php
function Tree($rootID, $database_jdt_cma, $jdt_cma)
{
    mysql_select_db($database_jdt_cma, $jdt_cma);
    $query_currentList = "SELECT page_id, page_title, page_parent_id, page_order FROM hks01_page WHERE page_parent_id = $rootID ORDER BY page_order ASC";
    $currentList = mysql_query($query_currentList, $jdt_cma) or die(mysql_error());
    $row_currentList = mysql_fetch_assoc($currentList);
    $totalRows_currentList = mysql_num_rows($currentList);
    
    do {
        $parent_id = $row_currentList['page_id']; // set curent row to parent
        echo( $row_currentList['page_title']."<br>\n" );
        
        // find children of current row
        $query_childList = "SELECT page_id, page_title, page_parent_id, page_order FROM hks01_page WHERE page_parent_id = $parent_id ORDER BY page_order ASC";
        $childList = mysql_query($query_childList, $jdt_cma) or die(mysql_error());
        $row_childList = mysql_fetch_assoc($childList);
        $totalRows_childList = mysql_num_rows($childList);
        
        if( $totalRows_childList != 0 ) {
            // echo( " - Total Children: ".$totalRows_childList."<br>\n" );
            return ( Tree($parent_id, $database_jdt_cma, $jdt_cma) );
        }
        
    } while ($row_currentList = mysql_fetch_assoc($currentList));
}

Tree(0, $database_jdt_cma, $jdt_cma);

?> 

___________________________________________


and returns the following...

___________________________________________

Home
Contact Us
___________________________________________

No other records show.

If I comment out the return statement I get...
___________________________________________

Home
Architectural Visualization
Web Design
HKS, Inc.
___________________________________________

All top level, but obviously no children

If I leave in the return statement but rearrange the while statement as follows
___________________________________________

<?php
function Tree($rootID, $database_jdt_cma, $jdt_cma)
{
    mysql_select_db($database_jdt_cma, $jdt_cma);
    $query_currentList = "SELECT page_id, page_title, page_parent_id, page_order FROM hks01_page WHERE page_parent_id = $rootID ORDER BY page_order ASC";
    $currentList = mysql_query($query_currentList, $jdt_cma) or die(mysql_error());
    $row_currentList = mysql_fetch_assoc($currentList);
    $totalRows_currentList = mysql_num_rows($currentList);
    
    while ($row_currentList = mysql_fetch_assoc($currentList)) {
        $parent_id = $row_currentList['page_id']; // set curent row to parent
        echo( $row_currentList['page_title']."<br>\n" );
        
        // find children of current row
        $query_childList = "SELECT page_id, page_title, page_parent_id, page_order FROM hks01_page WHERE page_parent_id = $parent_id ORDER BY page_order ASC";
        $childList = mysql_query($query_childList, $jdt_cma) or die(mysql_error());
        $row_childList = mysql_fetch_assoc($childList);
        $totalRows_childList = mysql_num_rows($childList);
        
        if( $totalRows_childList != 0 ) {
            //echo( " - Total Children: ".$totalRows_childList."<br>\n" );
            return ( Tree($parent_id, $database_jdt_cma, $jdt_cma) );
        }
        
    }
}

Tree(0, $database_jdt_cma, $jdt_cma);

?> 

___________________________________________

This gives me...
___________________________________________

Architectural Visualization
Rendering
Animation
Staff
___________________________________________

Skips the first record, gives the second record, all it's children, and then stops.

Somebody, please help me, I'm relatively new to PHP and have been mucking with this for days.

Thanks in advance for any help y'all can give me.

JD-

Is This A Good Question/Topic? 0
  • +

Replies To: recursive function problem

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: recursive function problem

Posted 18 August 2005 - 11:25 AM

I've been staring at code all day, so forgive me if I missed this, but I didn't see you calling Tree() from within Tree(). A recursive function operates by calling the function from within itself. You usually run a top level query outside the function for all top level items (items that aren't children), then loop through the query calling Tree() each time.

If that's not a good explanation, lemme know and I'll find some real code for you to look at.
Was This Post Helpful? 0
  • +
  • -

#3 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: recursive function problem

Posted 18 August 2005 - 11:39 AM

He calls tree at the very end in the return.
Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: recursive function problem

Posted 18 August 2005 - 11:57 AM

Ah, well, in that case... I don't know right off hand. Anyone else wanna take a gander?
Was This Post Helpful? 0
  • +
  • -

#5 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: recursive function problem

Posted 18 August 2005 - 12:38 PM

You may want to try something along the lines of getting the data all out in one sql statement instead of making a new one for parent? Is that even possible?

I think group by and an advanced use of order by might work.
Was This Post Helpful? 0
  • +
  • -

#6 jasdulan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-August 05

Re: recursive function problem

Posted 18 August 2005 - 01:00 PM

Following some advice in another forum, I removed "return" from the last call to Tree, and it fixed the sub return, then I added another while loop for the children but now I get another crazy print. I think there's an error with my passed id - $parent_id. It's looping the full child list for the amount of children and then stopping, not going down all the way.

Back to debugging...

Following is the new code and the result.

<?php
	function Tree($rootID, $database_pisqlMYSQL, $pisqlMYSQL)
	{
  mysql_select_db($database_pisqlMYSQL, $pisqlMYSQL);
  $query_currentList = "SELECT page_id, page_title, page_parent_id, page_order FROM pisql_page WHERE page_parent_id = $rootID ORDER BY page_order ASC";
  $currentList = mysql_query($query_currentList, $pisqlMYSQL) or die(mysql_error());
  $row_currentList = mysql_fetch_assoc($currentList);
  $totalRows_currentList = mysql_num_rows($currentList);
  
  do {
 	 $parent_id = $row_currentList['page_id']; // set curent row to parent
 	 echo( $row_currentList['page_title']."<br>\n" );
 	 
 	 // find children of current row
 	 $query_childList = "SELECT page_id, page_title, page_parent_id, page_order FROM pisql_page WHERE page_parent_id = $parent_id ORDER BY page_order ASC";
 	 $childList = mysql_query($query_childList, $pisqlMYSQL) or die(mysql_error());
 	 $row_childList = mysql_fetch_assoc($childList);
 	 $totalRows_childList = mysql_num_rows($childList);
 	 
 	 if( $totalRows_childList != 0 ) {
   	 echo( ": ".$totalRows_childList."<br>\n" );
   	 do {
   	 echo( "&nbsp&nbsp&nbsp&nbsp&nbsp" );
   	 Tree($parent_id, $database_pisqlMYSQL, $pisqlMYSQL);
   	 } while ($row_childList = mysql_fetch_assoc($childList));
 	 }
 	 
  } while ($row_currentList = mysql_fetch_assoc($currentList));
	}
	
	Tree(0, $database_pisqlMYSQL, $pisqlMYSQL);

?>


And the result...

Home
: 1
     Contact Us
Architectural Visualization
: 4
     Tridot
Rendering
Animation
Staff
     Tridot
Rendering
Animation
Staff
     Tridot
Rendering
Animation
Staff
     Tridot
Rendering
Animation
Staff
Web Design
: 2
     Design
Production
     Design
Production
HKS, Inc.


Was This Post Helpful? 0
  • +
  • -

#7 malkiri  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 364
  • Joined: 29-March 01

Re: recursive function problem

Posted 18 August 2005 - 03:29 PM

Generally speaking, your problem is that the actions your function is supposed to do isn't really well-defined. You kind of have it handling both the root node and its children at each level. It'll help to write out some pseudocode to help get things clearer:

Quote

function Tree(root_node_id)
  Print(root_node)
  Query the database for the rows where page_parent_id = root_node_id
  For all matching nodes,
    Tree(node_id)


I'd suggest rewriting your function to reflect this pseudocode. Also, if I remember correctly, you don't need to mysql_select_db at each call to the function - one call right before the first call to Tree ought to be good.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1