I think it will work if you return true, and just modify the global
variable (not try to output it).
Then output it (or capture it to begin with ($total_count =
OneHundred();) after the function is called.
You could also pass the variable, rather than making it global
(OneHundred("0") initially, and OneHundred($count) from within the
function).
For instance:
<?php
function OneHundred(){
global $count;
if($count < 100){
$count++;
OneHundred();
}
return true;
}
OneHundred();
echo $count;
?>
Hope that helps,
Brian
> -----Original Message-----
> From: Andy Crain [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, January 22, 2003 1:23 PM
> To: [EMAIL PROTECTED]
> Subject: [PHP] Returning a value from a recursive function
>
>
> Everyone,
>
> I'm stumped by this, even after searching the archives and the web,
> although I admit the solution likely is something very obvious.
>
>
>
> I've written a function to build a string of "breadcrumb" links for a
> web site directory similar to Yahoo, etc. It queries a
> categories table
> recursively until it reaches the root category, building a string of
> categories from the current category all the way back up to root. The
> function seems to work fine if I output directly from it
> using echo, but
> if I instead try to return the string to the global scope and
> then echo
> it, I get nothing. The line where I use echo/return is
> indicated below.
>
> Thanks,
>
> Andy
>
>
>
> function breadcrumbs($category_id=0,$mode='linked'){
>
> global $id_array,$name_array;
>
>
>
> static $counter = 0;
>
>
>
> if ($category_id == 0) {
>
> //once we're down to the root, build a return string
>
> if ($mode == 'linked') {
>
> $output = '<A HREF="' . $_SERVER['PHP_SELF'] . '">HOME</A>';
>
> } else {
>
> $output = 'HOME';
>
> }
>
> while($counter > 0){
>
> if ($mode == 'linked') {
>
> $output .= ' > <A HREF="' . $_SERVER['PHP_SELF'] .
> '?category_id=' . array_pop($id_array) . '">' .
> array_pop($name_array)
> . '</A>';
>
> } else {
>
> $output .= ' > ' . array_pop($name_array);
>
> }
>
> $counter--;
>
> }
>
> return $output; /*<--------HERE: IF I SUBSTITUTE "ECHO" FOR
> "RETURN", EVERYTHING WORKS; BUT WITH "RETURN", IT DOESN'T WORK*/
>
> } else {
>
> $sql = 'SELECT category_id, parent_cat, category_name FROM
> web_categories WHERE category_id = ' . $category_id;
>
> $result = safe_query($sql);
>
> while($query_data = mysql_fetch_assoc($result)){
>
> $id_array[$counter] = $query_data['category_id'];
>
> $name_array[$counter] = $query_data['category_name'];
>
> //now, move one step up and make the current category the
> previous parent
>
> $category_id = $query_data['parent_cat'];
>
> $counter++;
>
> breadcrumbs($category_id,$mode);
>
> }
>
> }
>
> }
>
>
>
> $show = breadcrumbs(9);
>
> echo $show;
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php