|
-
Jan 26th, 2004, 01:48 AM
#1
Thread Starter
Fanatic Member
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>>>";
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.
-
Jan 26th, 2004, 05:12 AM
#2
Thread Starter
Fanatic Member
ah just realized my error:
$row['id'],
should have been
$row['category_id'];
If wishes were fishes we'd all cast nets.
-
Jan 27th, 2004, 08:22 AM
#3
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.
-
Jan 27th, 2004, 08:44 AM
#4
Thread Starter
Fanatic Member
Yea, they were a throw back from when I was trying something else.
If wishes were fishes we'd all cast nets.
-
Feb 6th, 2004, 05:23 PM
#5
Frenzied Member
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.
-
Feb 6th, 2004, 05:30 PM
#6
Thread Starter
Fanatic Member
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.
-
Feb 6th, 2004, 05:42 PM
#7
Frenzied Member
yeah, using "traversal algorithm"
http://www.sitepoint.com/article/1105/1
much easier and a lot more flexable.
-
Feb 6th, 2004, 05:54 PM
#8
Thread Starter
Fanatic Member
You still need to use recursion in the rebuild tree method
If wishes were fishes we'd all cast nets.
-
Feb 6th, 2004, 07:27 PM
#9
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|