Page 1 of 1
problem with recursiv function
Posted: Tue Aug 22, 2006 8:43 am
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);
Posted: Tue Aug 22, 2006 10:36 am
by Ollie Saunders
Could you please show me the structure of the table:
and a complete example of the kind of output you would like to create.
Thanks
Re: problem with recursiv function
Posted: Tue Aug 22, 2006 11:15 am
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:
to:
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?
Posted: Tue Aug 22, 2006 12:31 pm
by Ollie Saunders
global $contor++;
E_FATAL syntax error
Posted: Tue Aug 22, 2006 1:09 pm
by blackbeard
I don't use globals that often, need to change it to?:
global $contor;
$contor++;
Posted: Wed Aug 23, 2006 3:41 am
by CoderGoblin
For things like this I generally use the "
static" declaration within the function rather than global.