Welcome Guest, Not a member yet? Register   Sign In
Returning menu structure.
#1

[eluser]Unknown[/eluser]
Hi Again, This little function loops though a multidimensional array and echos the data out as a multilevel list. It works a treat but instead of echoing the HTML list i would like it to return the HTML i have tried with a snippet below but it doesn't display the menu correctly.


Code that works fine.
Code:
/*
  * Render Cats into list.
  *************************/

function renderCatsList($arr)
{
     echo "<ul>";
  
     foreach ($arr as $cat)
     {
      echo "<li>";
      if(!isset($cat['children'])) $cat['children'] = array();
         echo $cat['cat_title'];

         if (count($cat['children']) > 0)
         {
          $this->renderCatsList($cat['children']);
         }

         echo "</li>";
     }

     echo "</ul>";

    }

But i would prefer this method but this does not work.
Code:
/*
  * Render Cats into list.
  *************************/

function renderCatsList($arr)
{
  $output = "<ul>";
  
     foreach ($arr as $cat)
     {
      $output .=  "<li>";
      if(!isset($cat['children'])) $cat['children'] = array();
         $output .= $cat['cat_title'];

         if (count($cat['children']) > 0)
         {
          $this->renderCatsList($cat['children']);
         }

         $output .= "</li>";
     }

        $output .= "</ul>";
            return $output;

    }

Regards Script Gecko :coolmad:
#2

[eluser]skunkbad[/eluser]
put return in front of $this->renderCatsList($cat['children']);
#3

[eluser]Aken[/eluser]
[quote author="skunkbad" date="1351216137"]put return in front of $this->renderCatsList($cat['children']);[/quote]
Nah, should be
Code:
$output .= $this->renderCatsList($cat['children']);
#4

[eluser]skunkbad[/eluser]
[quote author="Aken" date="1351222430"][quote author="skunkbad" date="1351216137"]put return in front of $this->renderCatsList($cat['children']);[/quote]
Nah, should be
Code:
$output .= $this->renderCatsList($cat['children']);
[/quote]

Yeah, that!
#5

[eluser]Aken[/eluser]
Tongue
#6

[eluser]skunkbad[/eluser]
Recursive functions are always a joy ... NOT! The one above is simple enough, but whenever I find myself doing one of great complexity, I think it adds a couple of grey hairs to my head.




Theme © iAndrew 2016 - Forum software by © MyBB