![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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! |
|
|
|
|
|
#2 |
|
PFO Founder
![]() ![]() |
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. |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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).
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Feb 2005
Location: PA, USA
Posts: 253
Rep Power: 4
![]() |
link's broke >.< it's good practice not to use spaces in your url
![]() nice code though, looks good. |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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/ |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|