Generating your website menu using SimpleXML and PHP

I recently reviewed the design of the dynamic menu used by Mandurah Web for navigation around their website. The decision to update the way the menu was generated fell down to a few reasons:

  1. A XML menu would be easier to maintain
  2. The menu file could be used to generate a sitemap.
  3. The menu file could be used to generate a Google sitemap.

If you have ever seen an XML file you probably know what I mean when I say easy to maintain. It reads very much like English and is on the most part easily understood. If you are new to XML keep reading for an explanation further down.

Using the same menu file to generate the sitemap on the fly makes life easier for maintenance of the site. Any new pages that are added to the site’s menu will automatically be reflected on the sitemap.

Generating a Google sitemap from their own website’s admin panel was far easier than having to use external tools to generate the sitemap, then download the file and upload to their own server. It would be as simple as running the script on the server and the new sitemap would be in place.

So a bit of a different approach here, for all those who are impatient and don’t want an explanation of how this works and just want to play with the code, here are the two files, including both XML and the generateMenu(), generateSitemap and createSitemap() methods.

XML (menu.xml)


	

			
			Home
index.php

			About
about.php
				

					
					learn more about us?
us.php

			Contact
contact.php

					Webmaster
contact.php?who=webmaster

					Sales
contact.php?who=sales

PHP Generate Menu

function generateMenu2()
{
	$menu = simplexml_load_file("menu.xml");
	echo "

";
}

If you are still reading here then you are probably looking for some explanations so let’s start with the XML menu file.

XML or extensible mark-up language is a very powerful tool and should definitely be part of any web developer’s toolkit. The simplest way to explain it is probably with an example, bearing in mind that XML allows you to create your own custom mark-up tags, all tags must be closed and each set of tags or node follows a child / parent relationship.


	

			
			Home
index.php

Each Item will become a top level menu item, with the corresponding name and link values. To add child items to the main items we simply next subitem tags within the item tag with their own name and link values like so.


	

			
			Home
index.php

			About
about.php
				

					
					learn more about us?
us.php

The generateMenu()  method is very simple but nevertheless produces effective results. The first step is to load the xml file using simplexml_load_file() which gives us a SimpleXML object to work with. We then loop through each elements child elements accessing their name and link properties. $menu->children() would be the same as $menu->item in our case, but using the children() method will return the children nodes whatever you have called them.

For the next loop we now use $child->children() again, this allows you to name your sub item’s as you please but unfortunately creates an extra if statement inside the for loop. You could quite easily remove the if statement and simply have $child->subitem.

All the code within both loops is quite straight forward, simply displaying out the menu as an un-ordered list and linking each link to its corresponding file noted in the XML file.

The code is well commented so I am sure most people will have no problems implementing this on their own site. I am aware there could be other / better ways to do this using recursion and would be happy if anyone is willing to share their own functions with us.

If you would like to see the menu live you can visit Mandurah Web Designs

P.S I also have the function I used to create the sitemap on the server and would be willing to share with anyone who was interested just contact me or request an article if you would like an explanation with it.

P.P.S I don't seem to be having much look with the formatting of code so if you would rather just download the files here they are.

xmlMenu.rar

Comments

Permalink

I have fixed the link. I have also attached the download in an email and sent it through to you directly.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.