How can I get the return value of a function if it is a recursive function? See below:
This function creates a page path that shows your location and the parent categories, based on the category you are in. The category names are linked to the appropriate categories except the one you are currently in.
Like this:
Home > Desktop Computers > PCs > Compaq
When I use echo before "$location", it works fine:
function displayLocation( $id )
{
global $cat, $PHP_SELF;
if(isset($id))
{
$result = mysql_query( "SELECT cat_name, root_id, papa_id FROM category WHERE root_id=$id" ) or error( mysql_error() );
$row = mysql_fetch_array( $result );
if( $row['papa_id'] == 0 )
{
if( $row['root_id'] == $cat ) $location = "<a href=\"$PHP_SELF\">Home</a> > {$row['cat_name']}";
else $location = "<a href=\"$PHP_SELF\">Home</a> > <a href=\"$PHP_SELF?cat={$row['root_id']}\">{$row['cat_name']}</a>";
}
else
{
if( $row['root_id'] == $cat ) $location = " > {$row['cat_name']}";
else $location = " > <a href=\"$PHP_SELF?cat={$row['root_id']}\">{$row['cat_name']}</a>";
displayLocation($row['papa_id']);
}
echo "<b>$location</b>\n";
}
}
displayLocation($cat)
But when I try to get the return value instead of the echo, it just doesn't work, it prints only the current category like this "> Compaq"
function displayLocation( $id )
{
global $cat, $PHP_SELF;
if(isset($id))
{
$result = mysql_query( "SELECT cat_name, root_id, papa_id FROM category WHERE root_id=$id" ) or error( mysql_error() );
$row = mysql_fetch_array( $result );
if( $row['papa_id'] == 0 )
{
if( $row['root_id'] == $cat ) $location = "<a href=\"$PHP_SELF\">Home</a> > {$row['cat_name']}";
else $location = "<a href=\"$PHP_SELF\">Home</a> > <a href=\"$PHP_SELF?cat={$row['root_id']}\">{$row['cat_name']}</a>";
}
else
{
if( $row['root_id'] == $cat ) $location = " > {$row['cat_name']}";
else $location = " > <a href=\"$PHP_SELF?cat={$row['root_id']}\">{$row['cat_name']}</a>";
displayLocation($row['papa_id']);
}
return "<b>$location</b>\n";
}
}
echo displayLocation($cat)
What is the problem? Please help. I need return value because I use function in a template file that is opened and echoed into a .php file. In my other functions I just replaced the echo with a value then I returned the value at the and of the function and it worked.
Thanks