Welcome Guest, Not a member yet? Register   Sign In
Recursive function in helper causes "Blank page"
#1

[eluser]Unknown[/eluser]
Hello, I can't fix this problem... I suspect it's bound to a bug. I hope someone will explain me that it's not a bug, and how to solve it Smile

I pass this array to a view (it's a small category tree - for non-italian readers: "idMadre" is the parent_id and "titolo" is the title):
Code:
Array
(
    [0] => Array
        (
            [idItem] => 22506
            [idMadre] => 0
            [titolo] => Vini
            [children] => Array
                (
                    [0] => Array
                        (
                            [idItem] => 22436
                            [idMadre] => 22506
                            [titolo] => Vini Rossi
                            [children] => Array
                                (
                                )

                        )

                )

        )

)

Then, in my view:
Code:
if(isset($product_categories)){
  listCats_Single(0, $product_categories, '');
}

"listCats_Single" function is in a custom helper:
Code:
function listCats_Single($parent, $myArray, $titoloMadre){
  $has_childs = false;
  $style='';

  foreach($myArray as $key => $value){
    if($value['idMadre'] == $parent){
      if($has_childs === false){
        $has_childs = true;
        echo '        <ul>' . "\n";
      }
      echo '        <li>' . "\n";
      if($parent==0){
        echo '          <span class="main">' . $value['titolo'] . '</span> '  . "\n";
      }
      else{
        echo '          + <span class="child">' . $value['titolo'] . '</span> '  . "\n";
      }

      listCats_Single($key, $myArray, $value['titolo']);
      echo '        </li>' . "\n";
    }
  }
  
  if($has_childs === true){
    echo '        </ul>' . "\n";
  }
}

Well, the recursion in listCats_Single seems to break everything, when I run the page it returns a blank answer (in Google Chrome: "net::ERR_EMPTY_RESPONSE"). If I remove the call to itself in the function:
Code:
listCats_Single($key, $myArray, $value['titolo']);
it restart to work.

I can't get any error, even if I raise the debug level in codeigniter application to "4".
#2

[eluser]Unknown[/eluser]
Maybe I solved with this modified version of the same function (I'm testing it to verify if it works). Anyway I think that the fact that the previous version doesn't show any error (infinite loop?) can be considered as a bug...?

Code:
function listCats_Single($parent, $myArray, $titoloMadre){
  $has_childs = false;
  $style='';

  foreach($myArray as $key => $value){
    if($value['idMadre'] == $parent){
      if($has_childs === false){
        $has_childs = true;
        echo '        <ul>' . "\n";
      }
      echo '        <li>' . "\n";
      if($parent==0){
        echo '          <span class="main">' . $value['titolo'] . '</span> '  . "\n";
      }
      else{
        echo '          + <span class="child">' . $value['titolo'] . '</span> '  . "\n";
      }

      if(count($value['children'])>0){echo
        listCats_Single($value['idItem'], $value['children'], $value['titolo']);
      }
      
      echo '        </li>' . "\n";
    }
  }
  
  if($has_childs === true){
    echo '        </ul>' . "\n";
  }
}
#3

[eluser]skunkbad[/eluser]
Helper functions aren't anything special, so don't blame CI. Take your helper function outside of CI, and you'll probably see you still have an error.

Maybe a recursive function like the one here will help:

https://bitbucket.org/skunkbad/community...y_menu.php




Theme © iAndrew 2016 - Forum software by © MyBB