Welcome Guest, Not a member yet? Register   Sign In
help with recursive function
#1

[eluser]kbauman[/eluser]
There's been a lot of discussion regarding the adjacency vs. nested sets method, for hierarchical data, so I know all about that. In fact I tried out both methods, and since I have a database I need to work with that uses the adjacency method I had to write a recursive function to put the data in order.

My problem is that if I put it in a library were I'd like it, or even in the model with the database functions, I get an error for the recursive calling of the function saying that it's not defined. Here's the function:

Code:
function create_menu($categories, $parent = 0, $i = 0, $base="gallery") {
      $result = array ();
        if ($categories) {
          foreach ($categories as $category) {
            if ($category->parent_id == $parent) {
                $level = $i;
                    $result[$category->id]['category'] = str_repeat("<ul><li>", ($level+1));
                    $result[$category->id]['category'] .= anchor("/$base/$category->category", "$category->category");
                    $result[$category->id]['category'] .= str_repeat("</ul></li>\n", ($level+1));
                    $children = create_menu($categories, $category->id, ($i+1), ($base."/".$category->category));
                    $result += $children;
            }
        }
        }
        return $result;
  }

What should I do? It only worked so far, when I was writing the function, if I had the database function, and this function all in the index function of my controller. I don't really want it there.
#2

[eluser]Crafter[/eluser]
If your function is a library, then its part of the library class, and hence must be called with the $this reference.
Code:
$this->create_menu()
#3

[eluser]kbauman[/eluser]
Hmm...I'll try again. When I did that, it said "$this->create_menu" not declared.




Theme © iAndrew 2016 - Forum software by © MyBB