EllisLab text mark
Advanced Search
     
help with recursive function
Posted: 31 August 2007 05:59 PM   [ Ignore ]
Joined: 2007-03-21
25 posts

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:

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.

 
Posted: 31 August 2007 08:07 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2006-06-17
151 posts

If your function is a library, then its part of the library class, and hence must be called with the $this reference.

$this->create_menu() 
 Signature 

CodeCrafter - Open Source Code Generation for CI

 
Posted: 31 August 2007 08:10 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2007-03-21
25 posts

Hmm…I’ll try again. When I did that, it said “$this->create_menu” not declared.