Recursive php menu function not playing ball..

Associate
Joined
5 Jun 2004
Posts
1,341
Location
Hythe, Hants
Ok here it is guys, what I'm trying to do here is create an unordered list with all the menu items in their relevent positions, the function works fine if I replace $output to echo, but seeing as I don't want this function run every time someone visits the website I just want it run on the admin side of things, so i thought lets try and dump the data into a file we can just "include" it on the website.

Like I said It runs fine if I replace $output with echo, but if I just return $output it only appears to go over the loop once.

Brain has hit a dead spot and i've been fiddling with it for hours now with varying results, none of which work :s

Any Ideas guys?


Code:
function rebuild_links($x = 0, $tmp) {
		$mysql = new MySql(); 
			$result = $mysql->query("SELECT * FROM menu_tree WHERE template='".$tmp."' AND parent_id= '".$x."' ORDER BY homepage DESC, sortorder ASC");
			
			if($result->num_rows() > 0) {
				$output .= "\n	<ul>\n";
				
				while($result->next()) {
						$output .= '		<li><a href="index.php?fn='.$result->row['filename'].'&amp;pid='.$result->row['id'].'">'.$result->row['menu_name'].'</a>';
							rebuild_links($result->row['id'], $tmp);
								$output .= "</li>\n";
				}
			   $output .= "	</ul>";
			}
			return $output;
			
		$mysql->close($result);
	}

I then run the function like so:

Code:
$output = rebuild_links(0, ''.TEMPLATE_NAME.'');
			
$fp = @fopen('../templates/'.TEMPLATE_NAME.'/navigation.php', 'w');
									if (!$fp) {
										echo 'Well bugger me sensless and call me mary! something wrong happend.';
									}
								fwrite($fp, $output);
								fclose($fp);
 
Last edited:
got it sorted now, code is messy, but I really cannot be bothered to clean it up yet.

As follows.

Code:
function rebuild_links2($x = 0, $a, $tmp) { 
                $mysql = new MySql(); 
                        $result = $mysql->query("SELECT * FROM menu_tree WHERE template='".$tmp."' AND parent_id= '".$x."' ORDER BY homepage DESC, sortorder ASC"); 
                        if($result->num_rows() > 0) { 
                        
                                $ip = "\n       <ul>\n"; 
                                if ($a==0) { 
                                        $fp = @fopen('../templates/'.TEMPLATE_NAME.'/pages/navigation.php', 'w'); 
                                } 
                                if ($a > 0) { 
                                        $fp = @fopen('../templates/'.TEMPLATE_NAME.'/pages/navigation.php', 'a'); 
                                } 
                                                fwrite($fp, $ip); 
                                                fclose($fp); 
                                
                                
                                while($result->next()) { 
                                        $string = $result->row['filename']; 
                                        $patterns[0] = '/.php/'; 
                                        $replacements[0] = ''; 
                                        $link = preg_replace($patterns, $replacements, $string); 
                                                $ip = '  <li><a href="index.php?fn='.$link.'&amp;pid='.$result->row['id'].'">'.$result->row['menu_name'].'</a>'; 
                                                        $fp = @fopen('../templates/'.TEMPLATE_NAME.'/pages/navigation.php', 'a'); 
                                                                fwrite($fp, $ip); 
                                                                fclose($fp); 
                                                
                                                if($result->num_rows() > 200) { 
                                                        usleep(500000); 
                                                } 
                                                $a++; 
                                                        rebuild_links2($result->row['id'], $a, $tmp); 
                                                                $ip =  "</li>\n"; 
                                                                        $fp = @fopen('../templates/'.TEMPLATE_NAME.'/pages/navigation.php', 'a'); 
                                                                                fwrite($fp, $ip); 
                                                                                fclose($fp); 
                                } 
                           $ip = "      </ul>"; 
                                 $fp = @fopen('../templates/'.TEMPLATE_NAME.'/pages/navigation.php', 'a'); 
                                        fwrite($fp, $ip); 
                                        fclose($fp); 
                        } 
                        
                $mysql->close($result); 
        }
 
Back
Top Bottom