Creating a dynamic nav bar include

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
leecurry
Forum Newbie
Posts: 1
Joined: Thu Dec 01, 2005 11:32 am

Creating a dynamic nav bar include

Post by leecurry »

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I am relatively new to PHP but have been sucessfully using an include with a checklink function that displays the down state of a navigation button if the $_SERVER=PHP_SELF. This works well if you don't have a secondary level of files (ie more than one page for a section), otherwise it falls flat.

This is my script FYI:
HTML Code:

Code: Select all

<?
//navbarINC.php
//Create an array to store the info
$aURL = array();
$aName = array();
$aTitle = array();

//populate the array
$aURL[0] = "index.php";
$aName[0] = "home";
$aTitle[0] = "Home Page";

$aURL[1] = "about.php";
$aName[1] = "about";
$aTitle[1] = "About ";

$aURL[2] = "faq.php"; 
$aName[2] = "faq";
$aTitle[2] = "Frequently Asked Questions";

$aURL[3] = "client_list.php";
$aName[3] = "client_list";
$aTitle[3] = "Client List";

$aURL[4] = "testimonials.php";
$aName[4] = "testimonials";
$aTitle[4] = "Testimonials";

$aURL[5] = "job_listings.php";
$aName[5] = "job_listings";
$aTitle[5] = "Job Listings";

$aURL[6] = "contact.php";
$aName[6] = "contact";
$aTitle[6] = "Contact us";

//start to build the header
$header = "<div id=\"pageHeader\">
			<table cellpadding=\"0\" cellspacing=\"0\">
			<tr><td><img src=\"images/nav/logo_shade.gif\" alt=\"\" /></td>";
	 

//loop through the array, checking for a match on the link
for($x = 0; $x < count($aURL); $x++)
{
   $myTest = checkLink($aURL[$x]);
   if(!$myTest)     //If the link does not match, print link to screen
   {
	  	$header .= "<td><a href=\"" . $aURL[$x] . "\"";
	  	$header .= " title=\" ".$aTitle[$x]." \" onmouseover=\"MM_swapImage('". $aName[$x] . "','','images/nav/".$aName[$x]."_on.gif',1)\" ";
	  	$header .= "onmouseout=\"MM_swapImgRestore()\"><img src=\"images/nav/".$aName[$x]."_off.gif\" alt=\" ".$aTitle[$x]." \" ";
		$header .= "name=\"".$aName[$x]."\" id=\"".$aName[$x]."\" /></a></td>";

   }else{
	  $header .= "<td><a href=\"" . $aURL[$x] . "\"><img src=\"images/nav/". $aName[$x] . "_down.gif\"  alt=\" ".$aTitle[$x]." \" /> </a></td>";

   }
}

$header .= "</tr>";   //finish up the header info
$header .= "</table></div>";

print $header;  //print the header



function checkLink($myURL)
{  //Returns true if current page matches URL, if it does, do NOT show it!
   $thisPage = $_SERVER['PHP_SELF']; //copy to easy variable
   $mySlash = strrpos($thisPage, "/");//Find char of last slash
   $thisPage = substr($thisPage,$mySlash + 1);  //Strip off all but the relative URL
   if($myURL == $thisPage)
   {
      return TRUE;
   }else{
      return FALSE;
   }
}

?>

Though I don't understand all the code, it works fine for relatively simply sites.
However, I need to create one with more than one level (ie 2 or three pages of one section).

I want to be able to control which button shows the down state by hard coding a variable to each page:
ie

HTML Code:

Code: Select all

$page_name="contactus";

and then modify the script above to recognize the variable rather than the PHP Self function.

I hope this is simple. I just don't know where to start.

Thanks


Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

First, create an associative (multi-) array with all your pages like so:

Code: Select all

$navigation = array(
  'home' => array('title' => 'Home', 'link' => 'home.php', 'children' => array(
      'subpage1' => array('title' => 'Home', 'link' => 'home.php')
      //more subpages...
    )
  )
  //more pages...
);
Then cycle through them recursively, printing out the links with variable indentation, depending on the "depth".

An example:

Code: Select all

function cycle_recursive ($tree, $depth = 0) {  

  $output = '';

  foreach ($tree as $branch) {
    $output .= str_repeat('&nbsp;', $depth).$branch['name']."<br />\n";
    if (is_array($branch['children'])) $output .= cycle_recursive($branch['children'], $depth+1);
  }

  return $output;
}
Post Reply