Hi

I am almost near to where I want to be but need a final push from someone out there please because my brain is now dead!

I have created this recursive function which loops through my database table and displays my navigation options. All is working fine except for when I have a 3rd tier (or more).

An example of where things are going wrong would be as follows..

These URL's work in my current function:

mydomain.com/products
mydomain.com/products/level-one-product

But when 'level-one-product' has a child, my function is showing the URL incorrectly as such:

mydomain.com/products/level-two-product

and as you can see it is removing the /level-one-product part of the URL.

Any help would be greatly appreciated and here is my function:


function build_navigation($parent, $urlarray) {

$qry 	= "SELECT * FROM sections WHERE childof = '$parent'";
$result = mysql_query($qry);
$count 	= mysql_num_rows($result);

if ($count > 0) {
	while ($row = mysql_fetch_array($result)) {
		$sectionid 	= $row['sectionid'];
		$title 		= $row['title'];
		$url 		= $row['url'];		// e.g. about-us or contact-us etc
		$childof	= $row['childof'];	// the Parent record sectionid

		if ($childof > 0) {
			$path = $urlarray[0] ."/". $url;
		} else {
			$path = $url;
		}

		if (in_array($url, $urlarray)) { // if it's a live option
			echo "<li><a class=\"active\" href=\"/$path\" title=\"title\">LIVE: $title</a></li>";
			build_navigation($sectionid, $urlarray);
		} else {
			echo "<li><a href=\"/$path\" title=\"title\">$title</a></li>";
		}

	}
	mysql_free_result($result);
} 

}

// call the function first time and set the parentid to be 0
// so I only display the parent options only
// $urlarray contains the exploded $_SERVER['REQUEST_URI']
build_navigation(0, $urlarray);

    First of all, I spent about a week getting this right!

    ...Go to bed! - Then, when you wake up with a fresh brain, read my next post... and study it... It works perfectly!

      
      // First, we start by passing the current CATEGORY_ID as the $parentId argument...
      
      function categoryTree($parentId = 0, $indent = '&gt;', $exclude = '', $categoriesArray = '', $includeItself = FALSE, $topText = "Top", $startFrom = 0)
      {  
      if (!is_array($categoriesArray)) $categoriesArray = array(); if ( (sizeof($categoriesArray) < 1) && ($exclude != '0') ) { $categoriesArray[] = array('id' => 0, 'text' => $topText); } if ($includeItself == TRUE && $parentId != 0) { list($this_category_name) = mysql_fetch_row(mysql_query("SELECT `catName` FROM `YOUR_CATEGORY_TABLE` WHERE `catId` = '".$parentId."' ORDER BY `catName` ")); $categoriesArray[] = array('id' => $parentId, 'text' => $this_category_name); //echo "This cat: $category<br />"; } $res = mysql_query("SELECT `catName`, `catId` FROM `YOUR_CATEGORY_TABLE` WHERE `parentId` = '".$parentId."' ORDER BY `catName` "); while ($categories = mysql_fetch_assoc($res)) { $categoriesArray[] = array('id' => $categories['catId'], 'text' => $indent . '&nbsp;' . $categories['catName']); if ($categories['catId'] != $parentId) { $categoriesArray = categoryTree($categories['catId'], "&nbsp;&nbsp;".$indent."&nbsp;", $exclude, $categoriesArray); } } return $categoriesArray; }

      Look at this, study it, any questions... Please ask.

        If you could add comments to the scrip that would help me break it down.

        Many Thanks

          Thanks but I have managed to plod on and get my function working with unlimited levels.

          Thanks for your help.

            4 days later

            To be honest, I know how frustrating it is creating a recursive function... It took me ages to get my head around it, but then I was quite tired when I started.

            Then I woke up the next day and with a fresh mind, it felt like child's play.

              Write a Reply...