Nav nodes associative array to unordered list via recursive method

I have an associative array which needs to be displayed as an unordere

Page 1 of 1

3 Replies - 3130 Views - Last Post: 16 March 2009 - 11:58 AM Rate Topic: -----

#1 AlienWebguy  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 84
  • Joined: 04-March 09

Nav nodes associative array to unordered list via recursive method

Post icon  Posted 15 March 2009 - 12:04 PM

Hey guys -

Ok guys I have a navigation (CSS <ul><li> etc drop menu) which needs to be populated dynamically. I have an associative array which holds each nav node ID, Title, and array(children).

My problem right now is that my script is either skipping a bunch of child nodes or putting tags in the wrong places. I've literally been on a trial/error debug run for like 12 hours on this and after 3 I took myself off the clock because I feel like a total noob for probably missing something really obvious.

Essentially my nav has 5 buttons - they are all nav nodes themselves, so in my class I just do a check to see if the nav node is one of the 'master' nodes and force it to be wrapped in a <ul> for css aesthetics. That might be screwing up the whole thing - my brain is so fried on this script...I really need another set of eyes looking at it.

Here's my class file (class.NavMenu.php). It extends class.Nav.php which just houses all the database queries for this nav and the subpage side-nav (not applicable here):

<?php

/** **************************************************************************
* NAV MENU CLASS MANAGES NAVIGATIONAL HEIRARCHY AND DISPLAY
******************************************************************************/
class NavMenu extends Nav{

	private $nn_id;
	private $nn_title;

	/** CONSTRUCTOR **********************************************************/
	public function __construct($nn_id, $nn_title, $children, $cat=NULL) {

		# INSTANTIATE NEW PAGE FOR DB METHODS
		$this->page = Page::instantiate();

		# GLOBALIZE ARGUMENT
		$this->nn_id = $nn_id;
		$this->nn_title = $nn_title;
		$this->children = $children;
		$this->depth = $depth;

		# CREATE ARRAY FOR CHILDREN
		$this->childlist = array();

		# INSTANTIATE NEW NAV OBJECT FOR THIS NODE
		$this->node = new Nav($this->nn_id);

		# IF THIS NODE IS ASSIGNED AS A CATEGORY, PULL FROM ITSELF AND NOT ITS PARENT
		if($cat)
			$SQL_cat = 'AND nn_wpc_id = '.$cat;

		# BUILD SQL
		$SQL = 'SELECT * FROM nav_nodes WHERE nn_parent = '.$nn_id.' '.$SQL_cat.' ORDER BY nn_parent ASC, nn_id ASC ';

		# QUERY DATABASE
		$rs = $this->page->db_query($SQL);

		# IF RECORDSET IS NOT EMPTY, CONTINUE
		$count = 0;
		if(is_array($rs)){

			# ITERATE RECORDSET
			foreach($rs as $row){

				# INSTANTIATE A NEW NAVMENU OBJECT RECURSIVELY
				$this->childlist[$count]= new NavMenu($row['nn_id'],$row['nn_title'], $this->page->db_count('nn_id','nav_nodes','nn_parent = '.$row['nn_id']));

				$count++;

			} // next

		}// end if is array

	} // end method


	/** METHOD DISPLAYS NAV TREE *********************************************/
	  public function display(){

		# BUFFER OUTPUT
		$tmp = '';

		//echo $this->page->print_html_r($this->childlist);

		# CALL DISPLAY ON EACH OF THIS NODES CHILDREN -- A NODE WILL ONLY HAVE CHILDREN IN ITS LIST IF EXPANDED
		$num_children = count($this->childlist);

		# OPEN UNORDERED LIST - IF THIS UL IS A NESTED UL, APPEND CLASS FOR CSS DROP MENU
		if($this->node->nn_parent != 0)
			$ul_class = 'class="menu_ul"';
		else
			$ul_class = '';

		# VERBOSE DEBUG OUTPUT
		// echo $this->node->nn_title.' has '.$num_children.' kids<br />';

		# IF THIS NODE HAS CHILDREN OR IF THIS IS A MASTER NODE - MASTER NODES ARE THE NAV BUTTONS AND NEED TO BE WRAPPED IN <UL> FOR THE CSS MENU
		if($num_children > 0 || $this->node->nn_parent == 0){
			$tmp.= NL.'<!-- OPEN UNORDERED LIST FOR '.strtoupper($this->node->nn_title).' -->'.NL.'<ul '.$ul_class.'>'.NL;

			# VERBOSE DEBUG OUTPUT
			echo $this->node->nn_title.' - UL just opened.<br />';
		}
		else{

			# VERBOSE DEBUG OUTPUT
			echo $this->node->nn_title.' - UL SKIPPED.<br />';
			return '';
		}

		# GRAB RECURSIVE PARENT BRANCH TO CREATE LINK PATH
		$branch = $this->get_recursive_parent_nodes($this->nn_id);
		$path = '';
		if(is_array($branch)){
			foreach($branch as $limb)
				$path.= $limb['nn_uri'].'/';
		}

		# STRIP TRAILING SLASH
		$path = rtrim($path,'/');

		# GRAB WEB PAGE URI IF WEB PAGE HAS BEEN ASSIGNED TO THIS NODE
		if($this->node->nn_wp_id > 0){
			$wp_uri = $this->page->db_lookup('wp_uri','web_pages','wp_id='.$this->node->nn_wp_id);
			$full_link = WWW.'/'.$path.'/'.$wp_uri;
		}

		# AUTOMATICALLY OVERRIDE WEB PAGE URI IF EXTERNAL LINK WAS PROVIDED
		if($this->node->nn_external_link)
			$full_link = $this->node->nn_external_link;

		# WRAP NODE DATA IN A LIST AND CHECK FOR MAIN NAV ELEMENTS
		$li_id = '';
		$a_class = '';
		$a_value = $this->node->nn_title;
		switch($this->node->nn_id){

			# HOME NAV BUTTON
			case 1:
				$li_id = 'id="menu_main_li_home"';
				$a_class = 'class="main"';
				$a_value = '<img src="'.WWW.'/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Home" />';
				break;

			# ABOUT US NAV BUTTON
			case 2:
				$li_id = 'id="menu_main_li_about"';
				$a_class = 'class="main"';
				$a_value = '<img src="'.WWW.'/images/clear.gif" style="width:180px; height:38px; border:0;" alt="About" />';
				break;

			# GALLERY NAV BUTTON
			case 3:
				$li_id = 'id="menu_main_li_gallery"';
				$a_class = 'class="main"';
				$a_value = '<img src="'.WWW.'/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Gallery" />';
				break;

			# RESOURCES NAV BUTTON
			case 4:
				$li_id = 'id="menu_main_li_resources"';
				$a_class = 'class="main"';
				$a_value = '<img src="'.WWW.'/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Resources" />';
				break;

			# CONTACT NAV BUTTON
			case 5:
				$li_id = 'id="menu_main_li_contact"';
				$a_class = 'class="main"';
				$a_value = '<img src="'.WWW.'/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Contact" />';
				break;

		} // end switch

		# INJECT NEW LIST ELEMENT INTO OUTPUT BUFFER
		$nn_title = $this->childlist[$i]->node->nn_title;

		$tmp.= NL.'<!-- OPEN LIST ELEMENT FOR '.strtoupper($this->node->nn_title).' -->'.NL;
		$tmp.= '<li '.$li_id.'>'.NL.'<a href="'.$full_link.'" '.$a_class.'>'.$a_value.'</a>'.NL;

		# CALL METHOD RECURSIVELY FOR EACH CHILD ARRAY PRESENT - ($this->node->nn_title IS NOT PRESENT IN THE CLOSING TAGS, BUT $this->childlist[$i]->node->nn_title IS... RED FLAG?
		for($i=0; $i<$num_children; $i++)
			  $tmp.= $this->childlist[$i]->display().'</li>'.NL.'<!-- CLOSE LIST ELEMENT FOR '.strtoupper($this->childlist[$i]->node->nn_title).' -->'.NL;

		# CLOSE UNORDERED LIST - PREVIOUS METHOD WAS RETURNED
		$tmp.= '</ul>'.NL.'<!-- CLOSE UNORDERED LIST FOR '.strtoupper($this->node->nn_title).' -->'.NL.NL;

		# RETURN OUTPUT
		return $tmp;

	} // end method

} // end class



In my inc.header.php file, I call the method like such:

/** INSTANTIATE NEW NAV MENU OBJECT ******************************************/
$menu = new NavMenu(0, '', 1);

/** DISPLAY MENU *************************************************************/
echo '<div id="menu">'.NL;
echo $menu->display();
echo '</div>'.NL;



Currently the output is this:
- UL just opened.
Home - UL just opened.
About - UL just opened.
Awards - UL SKIPPED.
Professional - UL SKIPPED.
Gallery - UL just opened.
Kitchens - UL just opened.
Minnetonka Kitchen - UL SKIPPED.
Traditional Kitchen - UL SKIPPED.
Lowry Hill - UL SKIPPED.
Baths - UL just opened.
Minnetonka Bath - UL SKIPPED.
Bryn Mawr Bath - UL SKIPPED.
Kenwood Bath - UL SKIPPED.
Closet - UL just opened.
Lowry Hill Closet - UL SKIPPED.
Additions - UL just opened.
Historic Addition - UL SKIPPED.
Living - UL just opened.
Lowry Hill Fireplace - UL SKIPPED.
Resources - UL just opened.
Contact - UL just opened.

That output is good news because it's acknowledging the nodes with children and opening UL's for them. Unfortunately, No markup or anything is showing up. Here's the output source (minus all the other page stuff):

<div id="menu">
 - UL just opened.<br />Home - UL just opened.<br />About - UL just opened.<br />Awards - UL SKIPPED.<br />Professional - UL SKIPPED.<br />Gallery - UL just opened.<br />Kitchens - UL just opened.<br />Minnetonka Kitchen - UL SKIPPED.<br />Traditional Kitchen - UL SKIPPED.<br />Lowry Hill - UL SKIPPED.<br />Baths - UL just opened.<br />Minnetonka Bath - UL SKIPPED.<br />Bryn Mawr Bath - UL SKIPPED.<br />Kenwood Bath - UL SKIPPED.<br />Closet - UL just opened.<br />Lowry Hill Closet - UL SKIPPED.<br />Additions - UL just opened.<br />Historic Addition - UL SKIPPED.<br />Living - UL just opened.<br />Lowry Hill Fireplace - UL SKIPPED.<br />Resources - UL just opened.<br />Contact - UL just opened.<br />

<!-- OPEN UNORDERED LIST FOR  -->
<ul >

<!-- OPEN LIST ELEMENT FOR  -->
<li >
<a href="" ></a>

<!-- OPEN UNORDERED LIST FOR HOME -->
<ul >

<!-- OPEN LIST ELEMENT FOR HOME -->
<li id="menu_main_li_home">
<a href="" class="main"><img src="http://www.michaelreel.com/_v2/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Home" /></a>
</ul>
<!-- CLOSE UNORDERED LIST FOR HOME -->

</li>

<!-- CLOSE LIST ELEMENT FOR HOME -->

<!-- OPEN UNORDERED LIST FOR ABOUT -->
<ul >

<!-- OPEN LIST ELEMENT FOR ABOUT -->
<li id="menu_main_li_about">
<a href="" class="main"><img src="http://www.michaelreel.com/_v2/images/clear.gif" style="width:180px; height:38px; border:0;" alt="About" /></a>
</li>
<!-- CLOSE LIST ELEMENT FOR AWARDS -->
</li>
<!-- CLOSE LIST ELEMENT FOR PROFESSIONAL -->
</ul>
<!-- CLOSE UNORDERED LIST FOR ABOUT -->

</li>
<!-- CLOSE LIST ELEMENT FOR ABOUT -->

<!-- OPEN UNORDERED LIST FOR GALLERY -->
<ul >

<!-- OPEN LIST ELEMENT FOR GALLERY -->
<li id="menu_main_li_gallery">
<a href="" class="main"><img src="http://www.michaelreel.com/_v2/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Gallery" /></a>

<!-- OPEN UNORDERED LIST FOR KITCHENS -->
<ul class="menu_ul">

<!-- OPEN LIST ELEMENT FOR KITCHENS -->
<li >
<a href="" >Kitchens</a>
</li>
<!-- CLOSE LIST ELEMENT FOR MINNETONKA KITCHEN -->

</li>
<!-- CLOSE LIST ELEMENT FOR TRADITIONAL KITCHEN -->
</li>
<!-- CLOSE LIST ELEMENT FOR LOWRY HILL -->
</ul>
<!-- CLOSE UNORDERED LIST FOR KITCHENS -->

</li>
<!-- CLOSE LIST ELEMENT FOR KITCHENS -->

<!-- OPEN UNORDERED LIST FOR BATHS -->
<ul class="menu_ul">

<!-- OPEN LIST ELEMENT FOR BATHS -->
<li >
<a href="" >Baths</a>

</li>
<!-- CLOSE LIST ELEMENT FOR MINNETONKA BATH -->
</li>
<!-- CLOSE LIST ELEMENT FOR BRYN MAWR BATH -->
</li>
<!-- CLOSE LIST ELEMENT FOR KENWOOD BATH -->
</ul>
<!-- CLOSE UNORDERED LIST FOR BATHS -->

</li>
<!-- CLOSE LIST ELEMENT FOR BATHS -->

<!-- OPEN UNORDERED LIST FOR CLOSET -->
<ul class="menu_ul">

<!-- OPEN LIST ELEMENT FOR CLOSET -->
<li >

<a href="" >Closet</a>
</li>
<!-- CLOSE LIST ELEMENT FOR LOWRY HILL CLOSET -->
</ul>
<!-- CLOSE UNORDERED LIST FOR CLOSET -->

</li>
<!-- CLOSE LIST ELEMENT FOR CLOSET -->

<!-- OPEN UNORDERED LIST FOR ADDITIONS -->
<ul class="menu_ul">

<!-- OPEN LIST ELEMENT FOR ADDITIONS -->
<li >
<a href="" >Additions</a>

</li>
<!-- CLOSE LIST ELEMENT FOR HISTORIC ADDITION -->
</ul>
<!-- CLOSE UNORDERED LIST FOR ADDITIONS -->

</li>
<!-- CLOSE LIST ELEMENT FOR ADDITIONS -->

<!-- OPEN UNORDERED LIST FOR LIVING -->
<ul class="menu_ul">

<!-- OPEN LIST ELEMENT FOR LIVING -->
<li >
<a href="" >Living</a>
</li>
<!-- CLOSE LIST ELEMENT FOR LOWRY HILL FIREPLACE -->

</ul>
<!-- CLOSE UNORDERED LIST FOR LIVING -->

</li>
<!-- CLOSE LIST ELEMENT FOR LIVING -->
</ul>
<!-- CLOSE UNORDERED LIST FOR GALLERY -->

</li>
<!-- CLOSE LIST ELEMENT FOR GALLERY -->

<!-- OPEN UNORDERED LIST FOR RESOURCES -->
<ul >

<!-- OPEN LIST ELEMENT FOR RESOURCES -->
<li id="menu_main_li_resources">
<a href="" class="main"><img src="http://www.michaelreel.com/_v2/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Resources" /></a>

</ul>
<!-- CLOSE UNORDERED LIST FOR RESOURCES -->

</li>
<!-- CLOSE LIST ELEMENT FOR RESOURCES -->

<!-- OPEN UNORDERED LIST FOR CONTACT -->
<ul >

<!-- OPEN LIST ELEMENT FOR CONTACT -->
<li id="menu_main_li_contact">
<a href="http://www.michaelreel.com/_v2/contact/contact-twin-cities-home-builder-and-remodeler-michael-reel" class="main"><img src="http://www.michaelreel.com/_v2/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Contact" /></a>
</ul>
<!-- CLOSE UNORDERED LIST FOR CONTACT -->

</li>
<!-- CLOSE LIST ELEMENT FOR CONTACT -->

</ul>
<!-- CLOSE UNORDERED LIST FOR  -->

</div>




Ok so, you'll notice that there are a bunch of nodes being closed </li> which are never actually opened. On line 89 of class.NavMenu.php I have a return ''; because my comprehension of recursion is to return if the recursive object does not meet the criteria. If I comment out that return line, all those missing children are now opened but everything is a big mess and out of order. Here's the html output of that:

<div id="menu">
 - UL just opened.<br />Home - UL just opened.<br />About - UL just opened.<br />Awards - UL SKIPPED.<br />Professional - UL SKIPPED.<br />Gallery - UL just opened.<br />Kitchens - UL just opened.<br />Minnetonka Kitchen - UL SKIPPED.<br />Traditional Kitchen - UL SKIPPED.<br />Lowry Hill - UL SKIPPED.<br />Baths - UL just opened.<br />Minnetonka Bath - UL SKIPPED.<br />Bryn Mawr Bath - UL SKIPPED.<br />Kenwood Bath - UL SKIPPED.<br />Closet - UL just opened.<br />Lowry Hill Closet - UL SKIPPED.<br />Additions - UL just opened.<br />Historic Addition - UL SKIPPED.<br />Living - UL just opened.<br />Lowry Hill Fireplace - UL SKIPPED.<br />Resources - UL just opened.<br />Contact - UL just opened.<br />

<!-- OPEN UNORDERED LIST FOR  -->
<ul >

<!-- OPEN LIST ELEMENT FOR  -->
<li >
<a href="" ></a>

<!-- OPEN UNORDERED LIST FOR HOME -->
<ul >

<!-- OPEN LIST ELEMENT FOR HOME -->
<li id="menu_main_li_home">
<a href="" class="main"><img src="http://www.michaelreel.com/_v2/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Home" /></a>
</ul>
<!-- CLOSE UNORDERED LIST FOR HOME -->

</li>

<!-- CLOSE LIST ELEMENT FOR HOME -->

<!-- OPEN UNORDERED LIST FOR ABOUT -->
<ul >

<!-- OPEN LIST ELEMENT FOR ABOUT -->
<li id="menu_main_li_about">
<a href="" class="main"><img src="http://www.michaelreel.com/_v2/images/clear.gif" style="width:180px; height:38px; border:0;" alt="About" /></a>

<!-- OPEN LIST ELEMENT FOR AWARDS -->
<li >
<a href="http://www.michaelreel.com/_v2/about-michael-reel/awards/michaelreel-building-and-remodeling-industry-awards" >Awards</a>
</ul>
<!-- CLOSE UNORDERED LIST FOR AWARDS -->

</li>

<!-- CLOSE LIST ELEMENT FOR AWARDS -->

<!-- OPEN LIST ELEMENT FOR PROFESSIONAL -->
<li >
<a href="http://www.michaelreel.com/_v2/about-michael-reel/professional/professional-builder-and-remodeler-association" >Professional</a>
</ul>
<!-- CLOSE UNORDERED LIST FOR PROFESSIONAL -->

</li>
<!-- CLOSE LIST ELEMENT FOR PROFESSIONAL -->
</ul>
<!-- CLOSE UNORDERED LIST FOR ABOUT -->

</li>
<!-- CLOSE LIST ELEMENT FOR ABOUT -->

<!-- OPEN UNORDERED LIST FOR GALLERY -->
<ul >

<!-- OPEN LIST ELEMENT FOR GALLERY -->
<li id="menu_main_li_gallery">
<a href="" class="main"><img src="http://www.michaelreel.com/_v2/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Gallery" /></a>

<!-- OPEN UNORDERED LIST FOR KITCHENS -->
<ul class="menu_ul">

<!-- OPEN LIST ELEMENT FOR KITCHENS -->
<li >
<a href="" >Kitchens</a>

<!-- OPEN LIST ELEMENT FOR MINNETONKA KITCHEN -->
<li >

<a href="http://www.michaelreel.com/_v2/gallery/kitchens/minnetonka-kitchen/minnetonka-modern-kitchen-remodel" >Minnetonka Kitchen</a>
</ul>
<!-- CLOSE UNORDERED LIST FOR MINNETONKA KITCHEN -->

</li>
<!-- CLOSE LIST ELEMENT FOR MINNETONKA KITCHEN -->

<!-- OPEN LIST ELEMENT FOR TRADITIONAL KITCHEN -->
<li >
<a href="http://www.michaelreel.com/_v2/gallery/kitchens/traditional-minnetonka-kitchen/minnetonka-traditional-kitchen-remodel" >Traditional Kitchen</a>
</ul>
<!-- CLOSE UNORDERED LIST FOR TRADITIONAL KITCHEN -->

</li>
<!-- CLOSE LIST ELEMENT FOR TRADITIONAL KITCHEN -->

<!-- OPEN LIST ELEMENT FOR LOWRY HILL -->
<li >
<a href="http://www.michaelreel.com/_v2/gallery/kitchens/lowry-hill/minneapolis-lowry-hill-kitchen-remodel" >Lowry Hill</a>
</ul>
<!-- CLOSE UNORDERED LIST FOR LOWRY HILL -->

</li>
<!-- CLOSE LIST ELEMENT FOR LOWRY HILL -->
</ul>
<!-- CLOSE UNORDERED LIST FOR KITCHENS -->

</li>
<!-- CLOSE LIST ELEMENT FOR KITCHENS -->

<!-- OPEN UNORDERED LIST FOR BATHS -->

<ul class="menu_ul">

<!-- OPEN LIST ELEMENT FOR BATHS -->
<li >
<a href="" >Baths</a>

<!-- OPEN LIST ELEMENT FOR MINNETONKA BATH -->
<li >
<a href="http://www.michaelreel.com/_v2/gallery/baths/minnetonka-bath/minnetonka-modern-bath-remodel" >Minnetonka Bath</a>
</ul>
<!-- CLOSE UNORDERED LIST FOR MINNETONKA BATH -->

</li>
<!-- CLOSE LIST ELEMENT FOR MINNETONKA BATH -->

<!-- OPEN LIST ELEMENT FOR BRYN MAWR BATH -->
<li >
<a href="http://www.michaelreel.com/_v2/gallery/baths/bryn-mawr-bath/minneapolis-bryn-mawr-bath-remodel" >Bryn Mawr Bath</a>
</ul>
<!-- CLOSE UNORDERED LIST FOR BRYN MAWR BATH -->

</li>
<!-- CLOSE LIST ELEMENT FOR BRYN MAWR BATH -->

<!-- OPEN LIST ELEMENT FOR KENWOOD BATH -->
<li >
<a href="http://www.michaelreel.com/_v2/gallery/baths/kenwood-bath/minneapolis-kenwood-bath-remodel" >Kenwood Bath</a>
</ul>
<!-- CLOSE UNORDERED LIST FOR KENWOOD BATH -->

</li>
<!-- CLOSE LIST ELEMENT FOR KENWOOD BATH -->
</ul>
<!-- CLOSE UNORDERED LIST FOR BATHS -->

</li>
<!-- CLOSE LIST ELEMENT FOR BATHS -->

<!-- OPEN UNORDERED LIST FOR CLOSET -->
<ul class="menu_ul">

<!-- OPEN LIST ELEMENT FOR CLOSET -->
<li >
<a href="" >Closet</a>

<!-- OPEN LIST ELEMENT FOR LOWRY HILL CLOSET -->

<li >
<a href="http://www.michaelreel.com/_v2/gallery/other/lowry-hill-closet/minneapolis-lowry-hill-master-closet-design-and-build" >Lowry Hill Closet</a>
</ul>
<!-- CLOSE UNORDERED LIST FOR LOWRY HILL CLOSET -->

</li>
<!-- CLOSE LIST ELEMENT FOR LOWRY HILL CLOSET -->
</ul>
<!-- CLOSE UNORDERED LIST FOR CLOSET -->

</li>
<!-- CLOSE LIST ELEMENT FOR CLOSET -->

<!-- OPEN UNORDERED LIST FOR ADDITIONS -->
<ul class="menu_ul">

<!-- OPEN LIST ELEMENT FOR ADDITIONS -->
<li >
<a href="" >Additions</a>

<!-- OPEN LIST ELEMENT FOR HISTORIC ADDITION -->
<li >
<a href="http://www.michaelreel.com/_v2/gallery/additions/historic-addition/minneapolis-lowry-hill-historic-family-room-addition" >Historic Addition</a>
</ul>
<!-- CLOSE UNORDERED LIST FOR HISTORIC ADDITION -->

</li>
<!-- CLOSE LIST ELEMENT FOR HISTORIC ADDITION -->
</ul>
<!-- CLOSE UNORDERED LIST FOR ADDITIONS -->

</li>
<!-- CLOSE LIST ELEMENT FOR ADDITIONS -->

<!-- OPEN UNORDERED LIST FOR LIVING -->
<ul class="menu_ul">

<!-- OPEN LIST ELEMENT FOR LIVING -->
<li >
<a href="" >Living</a>

<!-- OPEN LIST ELEMENT FOR LOWRY HILL FIREPLACE -->
<li >
<a href="http://www.michaelreel.com/_v2/gallery/living/lowry-hill-fireplace/custom-fireplace-design-and-install" >Lowry Hill Fireplace</a>
</ul>
<!-- CLOSE UNORDERED LIST FOR LOWRY HILL FIREPLACE -->

</li>
<!-- CLOSE LIST ELEMENT FOR LOWRY HILL FIREPLACE -->
</ul>
<!-- CLOSE UNORDERED LIST FOR LIVING -->

</li>
<!-- CLOSE LIST ELEMENT FOR LIVING -->
</ul>
<!-- CLOSE UNORDERED LIST FOR GALLERY -->

</li>
<!-- CLOSE LIST ELEMENT FOR GALLERY -->

<!-- OPEN UNORDERED LIST FOR RESOURCES -->
<ul >

<!-- OPEN LIST ELEMENT FOR RESOURCES -->
<li id="menu_main_li_resources">
<a href="" class="main"><img src="http://www.michaelreel.com/_v2/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Resources" /></a>
</ul>
<!-- CLOSE UNORDERED LIST FOR RESOURCES -->

</li>
<!-- CLOSE LIST ELEMENT FOR RESOURCES -->

<!-- OPEN UNORDERED LIST FOR CONTACT -->
<ul >

<!-- OPEN LIST ELEMENT FOR CONTACT -->
<li id="menu_main_li_contact">
<a href="http://www.michaelreel.com/_v2/contact/contact-twin-cities-home-builder-and-remodeler-michael-reel" class="main"><img src="http://www.michaelreel.com/_v2/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Contact" /></a>
</ul>
<!-- CLOSE UNORDERED LIST FOR CONTACT -->

</li>
<!-- CLOSE LIST ELEMENT FOR CONTACT -->
</ul>
<!-- CLOSE UNORDERED LIST FOR  -->

</div>





So that's that. I've done recursive functions before and never had this much trouble with them so I think I'm just missing something totally obvious. Any ideas of help would be amazing - this project has to go live tonight and this is all that's left. Go figure it's being stubborn at the last minute...

Thanks guys~
--S--

This post has been edited by AlienWebguy: 15 March 2009 - 02:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Nav nodes associative array to unordered list via recursive method

#2 roudard  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 143
  • Joined: 26-September 05

Re: Nav nodes associative array to unordered list via recursive method

Posted 16 March 2009 - 11:24 AM

did you manage to get to it to work ?
Was This Post Helpful? 0
  • +
  • -

#3 AlienWebguy  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 84
  • Joined: 04-March 09

Re: Nav nodes associative array to unordered list via recursive method

Posted 16 March 2009 - 11:52 AM

I did actually. I just deleted that whole class file and started over since I was to the point of having changed so many things to try to fix it that I lost track of things. The second time around I ended up coding it differently and slightly less verbose, and it worked how I had hoped.

Here's the new working class file:
<?php

/** **************************************************************************
* NAV MENU CLASS MANAGES NAVIGATIONAL HEIRARCHY AND DISPLAY
******************************************************************************/
class NavMenu extends Nav{

	private $nn_id;
	private $nn_title;

	/** CONSTRUCTOR **********************************************************/
	public function __construct($nn_id=0, $nn_title='', $children=0, $cat=NULL) {

		# INSTANTIATE NEW PAGE FOR DB METHODS
		$this->page = Page::instantiate();

		# GLOBALIZE ARGUMENT
		$this->nn_id = $nn_id;
		$this->nn_title = $nn_title;
		$this->children = $children;
		$this->depth = $depth;

		# DECLARE MASTER NODES
		/*
			1=Home
			2=About
			3=Gallery
			4=Resources
			5=Contact
		*/
		$this->master_nodes = array(1,2,3,4,5);

		# GRAB NODE HEIRARCHY
		$this->menu = $this->get_children_recursively($nn_id,0,$cat);

	} // end method


	/** METHOD DISPLAYS NAV TREE *********************************************/
	  public function display($array,$master_node=false){

		if(!is_array($array))
			return;

		# CHECK THIS NODE ID AGAINST MASTER NODE ARRAY
		if(in_array($array[0]['nn_id'],$this->master_nodes))
			$ul_class = '';
		else
			$ul_class = 'class="menu_ul"';

		# OPEN UNORDERED LIST AND ASSIGN CLASS IF NESTED
		echo NL.'<ul '.$ul_class.'>'.NL;

		# ITERATE EACH NODE IN THIS ARRAY
		foreach($array as $key=>$value){

			# GRAB BUTTON INFO
			$button = $this->get_button_info($value['nn_id']);

			# OPEN LIST ELEMENT
			echo '<!-- OPEN LI FOR '.strtoupper($value['nn_title']).' -->'.NL;
			echo '<li '.$button['li_id'].' '.$button['li_class'].'>'.NL;

			# IF NO LINK, STILL ANCHOR SO RETURN FALSE onclick
			if($button['return_false'])
				$return_false = 'onclick="return false;"';
			else
				$return_false = '';

			# IF THIS NODE HAS CHILD NODES
			if(is_array($value['children'])){

				# OUTPUT ACTUAL LINK
				echo '<a href="'.$button['full_link'].'" '.$button['a_class'].' '.$return_false.'>'.$button['a_value'].'</a>';

				# RECURSION - CALL METHOD ON ITSELF WITH NEW NESTED ARRAY
				$this->display($value['children']);

			} // end if is_array
			else{

				# OUTPUT ACTUAL LINK
				echo '<a href="'.$button['full_link'].'" '.$button['a_class'].' '.$return_false.'>'.$button['a_value'].'</a>';
			}

			# CLOSE LIST ELEMENT
			echo NL.'</li>'.NL;
			echo '<!-- CLOSE LI FOR '.strtoupper($value['nn_title']).' -->'.NL;

		} // next

		echo '</ul>'.NL.NL;

	} // end method


	/** FUNCTION POPULATES NODE ARRAY ****************************************/
	public function populate($array){

		# CREATE ARRAY OF NODES
		$this->node_array = $this->get_children($nn_id);

		# ITERATE NODES AND CREATE NEW WRAPPER ARRAY
		if(is_array($this->node_array)){

			foreach($this->node_array as $node)
				$this->nodes[] = array('nn_id'=>$node['nn_id'],'nn_title'=>$node['nn_title'],'nn_parent'=>$node['nn_parent'],'nn_wp_id'=>$node['nn_wp_id'],'nn_external_link'=>$node['nn_external_link'],'children'=>$this->get_children($node['nn_id']));
		}

	}

	/** FUNCTION MAKES IT ALL HAPPEN *****************************************/
	public function build(){

		$this->display($this->menu);

	} // end method

	/** METHOD GRABS HTML STUFF **********************************************/
	public function get_button_info($nn_id){

		# GRAB NODE INFO
		$node = new Nav($nn_id);

		# GRAB RECURSIVE PARENT BRANCH TO CREATE LINK PATH
		$branch = $this->get_recursive_parent_nodes($node->nn_id);
		$path = '';
		$full_link = '';
		if(is_array($branch)){
			foreach($branch as $limb)
				$path.= $limb['nn_uri'].'/';
		}

		# STRIP TRAILING SLASH
		$path = rtrim($path,'/');

		# GRAB WEB PAGE URI IF WEB PAGE HAS BEEN ASSIGNED TO THIS NODE
		if($node->nn_wp_id > 0){
			$wp_uri = $this->page->db_lookup('wp_uri','web_pages','wp_id='.$node->nn_wp_id);
			$full_link = WWW.'/'.$path.'/'.$wp_uri;
			$return_false = false;
		}
		else
			$return_false = true;

		# AUTOMATICALLY OVERRIDE WEB PAGE URI IF EXTERNAL LINK WAS PROVIDED
		if($node->nn_external_link){
			$full_link = $node->nn_external_link;
			$return_false = false;
		}

		# WRAP NODE DATA IN A LIST AND CHECK FOR MAIN NAV ELEMENTS
		$li_id = '';
		$li_class = '';
		$a_class = '';
		$a_value = '<span>'.$node->nn_title.'</span>';
		switch($node->nn_id){

			case 1:
				$li_id = 'id="menu_main_li_home"';
				$li_class = 'class="menu_button"';
				$a_class = 'class="main"';
				$a_value = '<img src="'.WWW.'/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Home" />';
				break;

			case 2:
				$li_id = 'id="menu_main_li_about"';
				$li_class = 'class="menu_button"';
				$a_class = 'class="main"';
				$a_value = '<img src="'.WWW.'/images/clear.gif" style="width:180px; height:38px; border:0;" alt="About" />';
				break;

			case 3:
				$li_id = 'id="menu_main_li_gallery"';
				$li_class = 'class="menu_button"';
				$a_class = 'class="main"';
				$a_value = '<img src="'.WWW.'/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Gallery" />';
				break;

			case 4:
				$li_id = 'id="menu_main_li_resources"';
				$li_class = 'class="menu_button"';
				$a_class = 'class="main"';
				$a_value = '<img src="'.WWW.'/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Resources" />';
				break;

			case 5:
				$li_id = 'id="menu_main_li_contact"';
				$li_class = 'class="menu_button"';
				$a_class = 'class="main"';
				$a_value = '<img src="'.WWW.'/images/clear.gif" style="width:180px; height:38px; border:0;" alt="Contact" />';
				break;

		} // end switch

		# RETURN ARRAY OF VALUES
		$array = array();
		$array['li_id'] = $li_id;
		$array['li_class'] = $li_class;
		$array['a_class'] = $a_class;
		$array['a_value'] = $a_value;
		$array['full_link'] = $full_link;
		$array['return_false'] = $return_false;

		return $array;

	} // end method

} // end class




This is the method in class.Nav.php that finally was able to pull the nodes and nest them in an array correctly:
/** METHOD GRABS ALL CHILDREN RECURSIVELY ********************************/
public function get_children_recursively($parent=0,$level=0,$cat=null){

	# GRAB FIRST LAYER OF KIDS
	$rs = $this->get_children($parent,$cat);

	# IF NO CHILDREN, GET OUT OF THIS RECURSION
	if(!is_array($rs))
		return '';

	# ITERATE RECORDSET
	foreach($rs as $row){

		$menu[] = array('nn_id'=>$row['nn_id'],'nn_title'=>$row['nn_title'],'children'=>$this->get_children_recursively($row['nn_id'],$level+1));

	} // next

	return $menu;

} // end method




And this is how i call it now in the header:
/** INSTANTIATE NEW NAV MENU OBJECT FOR EACH BUTTON **************************/
$home = new NavMenu(0,'','',1);
$about = new NavMenu(0,'','',2);
$gallery = new NavMenu(0,'','',3);
$resources = new NavMenu(0,'','',4);
$contact = new NavMenu(0,'','',5);

/** DISPLAY MENU *************************************************************/
echo '<div id="menu">'.NL;
echo $home->build();
echo $about->build();
echo $gallery->build();
echo $resources->build();
echo $contact->build();
echo '</div>'.NL;



Hope that helps someone with a similar issue in the future. Man.. I've been coding PHP for 10 years now and I've NEVER been so stuck as I was with this module. I had an old project which dealt with sockets which racked my brain a bit but seriously....glad its over. I now know 517 ways how not to make a nav haha.

--S--
Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Nav nodes associative array to unordered list via recursive method

Posted 16 March 2009 - 11:58 AM

That code looks much better. I remember reading over the first one and being reminded of myself trying to parse through an array full of data formatted as ID:qty (An example would be something like 2311:4), and trying to split that all off, have it look up the ID number in the database, replace the ID number with the name, format the text, then display it. My first attempt on that was after being up almost 30 hours, and it was an endless grouping of arrays. I think I got about 8 deep before I lost track of what I was doing and wondered what I was thinking. Scrapped it, started over from scratch, now it works like a charm.

Sometimes starting over really does work wonders :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1