Recursive php menu function not playing ball.. SOLVED

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AS_Platinum
Forum Newbie
Posts: 6
Joined: Mon Apr 04, 2005 2:18 pm

Recursive php menu function not playing ball.. SOLVED

Post by AS_Platinum »

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 pointers guys?

Code: Select all

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'].'&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: Select all

$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 by AS_Platinum on Sat May 27, 2006 9:58 pm, edited 1 time in total.
AS_Platinum
Forum Newbie
Posts: 6
Joined: Mon Apr 04, 2005 2:18 pm

Post by AS_Platinum »

okay I sorted it now,

Code is messey at the moment and will clean it up later, code as follows:

Code: Select all

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.'&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);
	}
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Code is messey at the moment and will clean it up later,
I don't have much sympathy with that statement. "Messy" code often doesn't work. Lazy people write messy code and don't clean it up before posting it and asking for help.

People with discipline clean it up first, and often don't have to ask for help because cleaning up the mess removed the problem.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

I normally dont like giving out complete working code as an answer to a problem, but since you're dealing with recursion (a fairly tricky subject) I'll do so. This code came from a class i'm working to generate various types of menus (accordion, css dropdowns, etc). I just copied the code straight from the class, so you will have to change all the class members to regular variables, and my script (currently) only accepts nested arrays as it's data source, but you should be able to adapt it for your purposes.

Code: Select all

function doHtml($list) {
      $this->level++;
      if ($this->level>4) die ("<div style='font-weight:bold;color:red;'>Maximum Level Of Nesting Is 4</div>");
      $levelId=($this->level==1)?$this->id:"easyMenu".$this->level;
      $this->html.='<ul id="'.$levelId.'">'."\n";
      foreach ($list as $title => $url) {
         if (is_array($url)) {
            $this->html.="\t<li><a href='#'>{$title}</a>";
            $this->doHtml($url);
            $this->html.="</li>";
         } else {
            $this->html.="\t<li><a href='#'>{$title}</a></li>";
         }
      }
      $this->html.='</ul>'."\n";
   }
}
$navList = array(
"Home" => "#",
"Canada" => "#",
"Medicine" => "#",
"Level One" => array(
   "Wheeee" => "#",
   "HOO HAA" => "#",
   "Level Two" => array(
      "What?" => "#",
      "ziggy Puff" => "#",
      "Level Three" => array(
         "Levdel Four" => "index.php",
         "Ldevel Four" => "inddex.php",
         "Levdedl Four" => "inddex.php",
         "Levedl Four" => "indedx.php"
         ),
      "Markie Rose" => "#",
      "Tell me Gun" => "#"
      ),
   "Believe" => "#",
   "Dike Pigs" => "#"
   ),
"About" => "#",
"F.a.Q" => "#"
);
AS_Platinum
Forum Newbie
Posts: 6
Joined: Mon Apr 04, 2005 2:18 pm

Post by AS_Platinum »

I don't have much sympathy with that statement. "Messy" code often doesn't work. Lazy people write messy code and don't clean it up before posting it and asking for help.

People with discipline clean it up first, and often don't have to ask for help because cleaning up the mess removed the problem.
Comments like that really arn't appreciated ie: no need for it. I wasn't asking for any kind of sympathy was I, the fact that I said:
Code is messey at the moment and will clean it up later, code as follows:
Dosn't sound like I was asking for any more help does it. So not really a case of being lazy, just a case of having a life and going to the pub with the Miss's which I feel is far more important than cleaning up a bit of code.

Just thought I'd post my workings so as no-one would spend the time and hassle testing it when the problem was solved wasting their time.

Not really trying to justify myself, just responding to a post that would probably get anyones back up!

nickvd Thanks for the snippet, will look into it during the daylight hours.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

I need to learn to keep my big mouth shurt and my opinions to myself.
Doesn't mean I don't think it, I just didn't need to say it.
Soogn
Forum Newbie
Posts: 11
Joined: Sat May 27, 2006 12:53 pm

Post by Soogn »

**Agrees With Bill H**
AS_Platinum
Forum Newbie
Posts: 6
Joined: Mon Apr 04, 2005 2:18 pm

Post by AS_Platinum »

**Agrees With Bill H**
In what way. Or do I need to write another essay?

This is going far too OT.
Post Reply