Recursive function adds data to existing string?

Discussion in 'PHP' started by fallenboy, Mar 22, 2010.

  1. #1
    Hi guys! I'm new here!

    I have a problem that I can't solve, no matter how hard I try. Maybe you can help me? :)

    I have an array and a function to create a multi-level menu out of this array. Array is not multi-level. The problem: when I echo in function, everything works fine, but when I try to add all data to a string (to return it later), it doesn't add strings that have been created by recursion.

    Code

    Array structure:
    
    $menu[$row['id']] = array('title' => $row['title'], 'app' => $row['app'], 'parent' => $row['parent'], 'position' => $row['position']);
    
    Code (markup):
    $menu = html_menu($menu(array), $parent(parent's id), $active(some id), $multilevel(one-level vs multi), $html = ''(empty container));

    Function:
    
    function html_menu($data, $parent, $active,  $multilevel, $html) { 
    		
    	foreach($data as $key => $value) {
    	
    		if($data[$key]['parent'] == $parent) {
    			
    			$activate = ($key == $active ? 'class="active"' : "");
    			$title = $data[$key]['title'];
    			
    			$html .= "<li>\n<a href=\"".MODULEURL.$data[$key]['app']."/index.php?action=open&amp;id=".$key."\"".$activate.">".$title."</a>\n";
    			
    			if($multilevel) html_menu($data, $key, $active, $multilevel, $html);
    			
    			$html .= "\n</li>\n"; 
    		}
    	}
    	
    	return $html;
    }
    
    Code (markup):
    I hope you understand what I'm trying to do :)

    Please do not tell me to query recursively from mysql, cause it takes wayyyyyyyyy longer :)

    Thanks guys!
     
    fallenboy, Mar 22, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    Try this one

    
    
    function html_menu($data, $parent, $active,  $multilevel, $html) { 
    		
    	foreach($data as $key => $value) {
    	
    		if($data[$key]['parent'] == $parent) {
    			
    			$activate = ($key == $active ? 'class="active"' : "");
    			$title = $data[$key]['title'];
    			
    			$html .= "<li>\n<a href=\"".MODULEURL.$data[$key]['app']."/index.php?action=open&amp;id=".$key."\"".$activate.">".$title."</a>\n";
    			
    			if($multilevel) { 
                               $html .= html_menu($data, $key, $active, $multilevel, $html);
                            }
    			
    			$html .= "\n</li>\n"; 
    		}
    	}
    	
    	return $html;
    }
    
    
    
    Code (markup):
     
    stephan2307, Mar 22, 2010 IP
  3. fallenboy

    fallenboy Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Nothing happens, sorry :(
     
    fallenboy, Mar 22, 2010 IP
  4. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #4
    can you paste the code when you actually call the function and how you try to display the result of the function?
     
    stephan2307, Mar 22, 2010 IP
  5. fallenboy

    fallenboy Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    on page:
    $sidebar['left'][0] = get_menu(false, $id , true);

    That calls out a function that gets all the results from DB and creates a non-multi-level array and passes it to the function above. That function returns the output (html formatted string), first function returns the string to sidebar['left'].

    The problem: recursive part of the function does not add created string to another string (i think .= does not work or something). Why I think that? Cause when switch $html .= "blah"; to echo "blah"; everything works.

    So --- wtf id going on with .= when function recursives itself?

    Thanks for helping :)
     
    fallenboy, Mar 22, 2010 IP