Results 1 to 9 of 9

Thread: Calling a function within itself *resolved*

  1. #1

    Thread Starter
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668

    Calling a function within itself *resolved*

    I'm having a problem with a peice of code, it will execute fine except it won't go into the call.

    Code:
    function GetSubCategory($id,$str,$folder)
    	{
    		$db = OpenDB();
    		
    		$r = mysql_query("SELECT category_id,name,folder FROM category WHERE category_id <> ".$id." AND category_parent_id = ".$id,$db);
    		
    		while($row = mysql_fetch_array($r))
    		{
    			$r2 = mysql_query("SELECT count(*) as pages FROM Page p, category c WHERE p.category_id = c.category_id AND c.category_id = ".$row['category_id'],$db);
    			$row2 = mysql_fetch_array($r2);
    			$pages = $row2['pages']-1;		//ignore the index.php
    			$folder += $row['folder']; 		//append new folder to existing
    			echo $str."<a href='".$folder."'>".$row['name']." (".$pages.")</a><br>\n";
    			$str += "<a href='".$folder."'>".$row['name']."</a>&gt;&gt;";
    			GetSubCategory($row['id'],$str,$folder);
    		}
    		CloseDB($db);
    	
    	}

    I don't get any warnings or error messages, it just stops.
    Last edited by Graff; Jan 26th, 2004 at 05:12 AM.
    If wishes were fishes we'd all cast nets.

  2. #2

    Thread Starter
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668
    ah just realized my error:

    $row['id'],

    should have been
    $row['category_id'];
    If wishes were fishes we'd all cast nets.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You should remove the OpenDB and CloseDB calls from the recursive function, you don't want to call them every time.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668
    Yea, they were a throw back from when I was trying something else.
    If wishes were fishes we'd all cast nets.

  5. #5
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    it is bad coding to call the same function in itself, it may get stuck in a loop and crash the server. just a bad idea all around.
    Last edited by phpman; Feb 6th, 2004 at 05:29 PM.

  6. #6

    Thread Starter
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668
    I avoid recursion where possible if you can think of a better way to have a function get an indefinite amount of sub-categories from any given point within the tree I would definitly like to hear it.
    If wishes were fishes we'd all cast nets.

  7. #7
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    yeah, using "traversal algorithm"

    http://www.sitepoint.com/article/1105/1

    much easier and a lot more flexable.

  8. #8

    Thread Starter
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668
    You still need to use recursion in the rebuild tree method
    If wishes were fishes we'd all cast nets.

  9. #9
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    the modified preorder tree traversal algorithm doesn't use it if you do it right. I don't have anything calling itself in mine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width