Suppose i have following recursive function(dummy):
function someRecursiceFunc($id){
if(/*check_if_call_is_first*/){
//want to do some operations here with the $id
}
$sql = "SELECT id, parent_id, title FROM some_table WHERE id = ".(int)$id;
$db_result = mysql_query($sql);
while($row = mysql_fetch_array($db_result)){
someRecursiveFunc($row['parent_id']);
}
}
i tried with the static $count = 0 & incremented in while() loop
but this caused problem in function call inside loop(as static variable preserves the value)
For example:
foreach($datas as $data){
someRecursiveFunc($data['id']);//value of $count went increasing
}
is there any techinque so that i can check for first call?
You should preprocess. If it’s in a class you could use public and private methods, you really need to give an example.
But in short, this is not possible. You call the function and it creates a new scope, so you need to pass info or use globals to track counts/booleans. You could do this with return values that accumulate but I don’t think that will help with pre processing.
@AnthonySterling:
Finally you did it.That’s what i want.
Cheers.
Another Question:
is there any performance issues with recursion with extra argument(as done by hash) vs static variable/array approach(as done by you)
for such case?
Recursion is not necessarily a big performance issue, in general it is memory intensive, but do you have these problems? There are already a number of solutions for dealing with hierarchical data, you don’t need to reinvent the wheel.
the one way to get rid of such model is to repalce with nested set model.
But i have to use the mentioned model as the client have such db structure.
thanks