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;

Reply via email to