problem with recursiv function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fabby
Forum Commoner
Posts: 62
Joined: Tue Feb 15, 2005 1:06 am
Location: Romania
Contact:

problem with recursiv function

Post by fabby »

i have show function that extract all comments in comentarii table, and each comment has a comment. This function show the name, in ierarhical mode, and the problem is: how can i show the level of comment..??

Code: Select all

function show( $id,$id_parent ){
   global $contor;
   $query_com="select * from comentarii where id_articol='$id' and id_parent='$id_parent'";
   $result_com=mysql_query($query_com);
   $num=mysql_num_rows($result_com);

   while($row_com=mysql_fetch_array($result_com)){
   
   $nume=$row_com['nume'];
      echo $data;
      show($_GET['id'],$id);   
   }
}

$contor=0;
show($_GET['id'],0);
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Could you please show me the structure of the table:

Code: Select all

SHOW COLUMNS FROM `comentarii`
and a complete example of the kind of output you would like to create.

Thanks
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Re: problem with recursiv function

Post by blackbeard »

fabby wrote:i have show function that extract all comments in comentarii table, and each comment has a comment. This function show the name, in ierarhical mode, and the problem is: how can i show the level of comment..??

Code: Select all

function show( $id,$id_parent ){
   global $contor;
   $query_com="select * from comentarii where id_articol='$id' and id_parent='$id_parent'";
   $result_com=mysql_query($query_com);
   $num=mysql_num_rows($result_com);

   while($row_com=mysql_fetch_array($result_com)){
   
   $nume=$row_com['nume'];
      echo $data;
      show($_GET['id'],$id);   
   }
}

$contor=0;
show($_GET['id'],0);
If you want to show how many times the function was called, I think you can just change the:

Code: Select all

global $contor;
to:

Code: Select all

global $contor++;
and then echo $contor to show the number of iterations.

However, looking at the function, it appears that you'll get yourself into an infinite loop. After the first iteration, every subsequent function call will equate to:

show($_GET['id'], $_GET['id']);

Is that the intent of the function?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

global $contor++;
E_FATAL syntax error
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

I don't use globals that often, need to change it to?:

global $contor;

$contor++;
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

For things like this I generally use the "static" declaration within the function rather than global.
Post Reply