About Menus and Site Maps
This section focuses on an xml file that holds details about all the pages in this site. From this file all the side menus, and the site index are produced dynamically, and a site map used by search engines to explore my site is also generated.
Part of the main site xml file: menu.xml
<menuItem id="3">
<title><a href="/coloredCode/">Colored Code</a></title>
<lastmod>2008-11-13</lastmod>
<description>This application is used to add colour to plane text PHP source code</description>
<menuItem id="1">
<title><a href="/coloredCode/">Colored Code Application</a></title>
</menuItem>
</menuItem>
The menus are driven using AJAX to request links, which are generated on the server using php. The php script loops through the xml using simplexml and creates links with graphics to point to the link or away depending on what sort of link it is.
Part of the displayMenu() function looping through the xml
// Loop through elements
foreach($xml->menuItem as $item)
{
// Look for menu item from main subject headings
if($item->attributes() == $menuId)
{
// Call for highlighted menu item
displayMenuItem("highlighted", $item->title->a->attributes(),
$item->title->a, 1);
// Now search all sub items
$subXml = $item;
// Loop through the sub items
foreach($subXml->menuItem as $subItem)
{
// Look for individual page and highlight if found
if($subItem->attributes() == $pageId)
{
displayMenuItem("selected", $subItem->title->a->attributes(),
$subItem->title->a, 3);
}
else
{
displayMenuItem("subMenu", $subItem->title->a->attributes(),
$subItem->title->a, 0);
}
}
}
else
{
displayMenuItem("highlighted", $item->title->a->attributes(),
$item->title->a, 2);
}
}
To see the full code creating the menus follow the links to the php code and javascript / AJAX code
The site index is dynamically generated when the user selects the page using similar php code to the menu system, however instead of creating links with graphics it formats some of the text for headings and adds descriptions.
The php code generating the site index
<?php
// Load the xml menu file into simpleXML
$xml = simplexml_load_file("menu.xml");
// Loop through file
foreach($xml->menuItem as $item)
{
// Send links and descriptions to the screen
echo "<h2>" . $item->title->a . "</h2>";
echo "<p>" . $item->description . "</p>";
// Use child nodes
$subXml = $item;
// Loop through child
foreach($subXml->menuItem as $subItem)
{
// Send to screen
echo "<a href=\"" . $subItem->title->a->attributes() . "\">"
. $subItem->title->a . "</a><br />";
}
}
?>
The site map begins as a dom object, which is eventually turned into xml. The php code creates the document and elements adding the attributes for the urlset element.
The creation of the dom object
// Create new dom object
$dom = new DomDocument('1.0');
$urlset = $dom->appendChild($dom->createElement('urlset'));
$urlsetAttribute = $urlset->appendChild($dom->createAttribute("xmlns"));
$urlsetAttribute->appendChild($dom->createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9"));
The code then loops through the main xml file using simplexml, adding text nodes to the dom document / object.
Finally the document is converted into xml and presented to the user as html, where it can be pasted into the sitemap.xml and placed into the root of the site.
Converting the dom object into xml and presenting to the display
// Convert the dom object into xml
$dom->formatOutput = true;
$xmlSitemap = $dom->saveXML();
// Present to display
echo "<pre><code>" . htmlentities($xmlSitemap) . "</code></pre>";


