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!
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:
function someRecursiceFunc($id, $call_is_first){
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'], false);
}
}
foreach($datas as $data){
someRecursiveFunc($data['id'], true);
}
The $count method works if you increment before the recursive call and decrement after. Then $count tells how many recursive calls are ahead of the current one in the call stack.
I'm on the fence as to whether that's better than adding a function parameter (as arborint did). The former has a nice kind of simplicity, but feels like a "hack" when compared to the latter.
tasairis wrote:The $count method works if you increment before the recursive call and decrement after. Then $count tells how many recursive calls are ahead of the current one in the call stack.
I'm on the fence as to whether that's better than adding a function parameter (as arborint did). The former has a nice kind of simplicity, but feels like a "hack" when compared to the latter.
tasairis wrote:The $count method works if you increment before the recursive call and decrement after. Then $count tells how many recursive calls are ahead of the current one in the call stack.
I'm on the fence as to whether that's better than adding a function parameter (as arborint did). The former has a nice kind of simplicity, but feels like a "hack" when compared to the latter.
Actually to keep an extra parameter out, I would set $call_is_first to default to true:
function someRecursiceFunc($id){
static $count = 0;
$count++;
if($count == 1){
//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']);
}
$count--;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
function someRecursiceFunc($id, $First){
if($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']);
}
}