Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 24th, 2004, 2:50 PM   #1
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Here's the code for my menu generator. Just change the XML and watch the world spin:

nav.php
<script type="text/javascript" src="nav.js"></script>

<style>
/* Navigation Bar */
#menus {
	float:  left;
	width:  128px;
	padding: 	4px;
	padding-left: 24px;
	text-align: 	left;
}

.menu, #menuheader {
	margin-left: -16px;
	font-weight: bold;
}

.menuitems {
	display: 	none;
	margin-left: 24px;
	font-weight: normal;
}

.menuitem {
	text-indent: -8px;
}

.item {
	font-weight: bold;
}

.showhide {
	vertical-align: middle;
}

#naverror {
	text-align: 	left;
}
</style>

<?php
$file = 'nav.xml';
$currentMenu = '';
$currentItem = '';
$link = false;

function navStartElement($parser, $name, $attrs) {
	global $currentMenu;
	global $link;
	
	switch ($name) {
 case "MENU":
 	echo '<div id="'.$attrs['ID'].'" class="menu">';
 	echo '<img src="images/expand.gif" id="'.$attrs['ID'].'Button" width="12" height="12" onclick="toggleMenu(\''.$attrs['ID'].'\');" alt="" class="showhide" /> ';
 	
 	if ($attrs['LINK'])
  echo '<a href="'.$attrs['LINK'].'">'.$attrs['CAPTION'].'</a>';
 	else
  echo $attrs['CAPTION'];
 	
 	echo '<div id="'.$attrs['ID'].'Items" class="menuitems">';
 	
 	if ($currentMenu)
  $currentMenu = $currentMenu.';'.$attrs['ID'];
 	else
  $currentMenu = $attrs['ID'];
 	break;
 
 case "ITEM":
 	if ($currentMenu == '')
  echo '<div class="item">';
 	else
  echo '<div class="menuitem">';	
 	
 	if ($attrs['LINK']) {
  echo '<a href="'.$attrs['LINK'].'" class="menulink">';
  $link = true;
 	}
 	else
  $link = false;
 	break;
 
 case "BLANK":
 	echo '<br />';
 	break;
	}
}

function navEndElement($parser, $name) {
	global $currentMenu;
	global $link;
	
	switch ($name) {
 case "MENU":
 	echo '</div></div>';
 	$currentMenu = substr($currentMenu, 0, strpos($currentMenu, ';'));
 	break;
 case "ITEM":
 	if ($link)
  echo '</a>';
 	echo '</div>';
 	break;
	}
}

function navCharacterData($parser, $data) {
	global $currentItem;
	
	$currentItem = $data;
	echo $data;
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "navStartElement", "navEndElement");
xml_set_character_data_handler($xml_parser, "navCharacterData");
@ $fp = fopen($file, "r");
if (!($fp))
	echo '<p id="naverror">We are unable to load the navigation menu. We apologise for the inconvenience.</p>';
else {
	while ($data = fread($fp, 4096)) {
 if (!xml_parse($xml_parser, $data, feof($fp))) {
 	printf('<p id="naverror">XML error: %s at line %d</p>',
    xml_error_string(xml_get_error_code($xml_parser)),
    xml_get_current_line_number($xml_parser));
 	break;
 }
	}
	xml_parser_free($xml_parser);
}
?>

nav.js
function toggleMenu(mnu) {
	var menu = document.getElementById(mnu + "Items");
	var image = mnu + "Button";

	var expand = "images/expand.gif";
	var collapse = "images/collapse.gif";
	
	if (menu.style.display == "block") {
 menu.style.display = "none";
 swapImages(image, expand);
	}
	else {
 menu.style.display = "block";
 swapImages(image, collapse);
	}
	
	return true;
}

function swapImages(currentImage, newImagePath) {
	var image = new Image();
	image.src = newImagePath;
	
	document.getElementById(currentImage).src = image.src;
}

nav.xml (Example)
<?xml version="1.0" encoding="ISO-8859-1"?>

<navigation>

<item id="home" link="?">Home</item>

<menu id="mnuTest" caption="Test Menu" link="?page=test">
	<menu id="mnuTestSub" caption="Submenu">
 <item link="?">Submenu Item 1</item>
 <item>Submenu Item 2</item>
 <item>Submenu Item 3</item>
	</menu>
	<item>Menu Item 1</item>
	<item link="?page=boo">Menu Item 2</item>
	<item>Menu Item 3</item>
</menu>

</navigation>

If IPB's crappy formatting annoys you, you can also get the separate pages from http://80.1.252.217/Nav/nav.php, http://80.1.252.217/Nav/nav.js and http://80.1.252.217/Nav/nav.xml. Have fun!
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 24th, 2004, 3:40 PM   #2
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,667
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
nice code. i will have to take a closer look at it later this weekend
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Nov 24th, 2004, 5:07 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Thanks. BTW, forgot to mention, for a working demonstration of this, check out http://80.1.252.217/St. Olave's/. You can steal expand.gif and collapse.gif from there as well (in the images subdir).
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 23rd, 2005, 1:06 AM   #4
melbolt
Professional Programmer
 
melbolt's Avatar
 
Join Date: Feb 2005
Location: PA, USA
Posts: 253
Rep Power: 4 melbolt is on a distinguished road
link's broke >.< it's good practice not to use spaces in your url

nice code though, looks good.
melbolt is offline   Reply With Quote
Old Feb 23rd, 2005, 12:05 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
It's broken because it's been moved 20 times since then :p

If you really wanna see it, it's at http://www.saintolaves.net/me/
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:36 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC