Generating site navigation with PHP and DOM

I just finished EasyNav, a PHP script that automatically highlights the current page in a site navigation.

All you need to use it is generate your navigation as a nested list of links and include EasyNav in your pages. The current link gets automatically replaced with a “strong” element and gets the ID “current”. The parent list item gets the ID “active”.

This allows you to use the CSS examples of Listamatic to style your navigation.

EasyNav also allows for different navigations to be generated, dependent on the ID of the root UL.

  • Ignacio

    Great script Chris! I’ve been looking for something like this for some time. One question, how do I apply this to a list without it been nested (no submenus)??
    Thank you!

  • Ignacio

    Great Script! Just what I need for this project I just started.
    Now, how can I use this for a non-nested list (just main items without submenus?)???
    Thank you!

  • http://www.estherfuldauer.com Esther

    Nice script. I would really like to use. How can I use it with wp Pages and Categories (wp 1.5)?
    Thanks for your help

  • Gerhard

    Hi, your script is just what I was looking for. But now I found a problem: it crashes as soon as I use these german-language-specific “umlauts” as for example in “Geschäftskunden”: Geschäftskunden

      .
      Any idea? Thanks Gerhard

  • yorgos

    Very nice and very useful script!!

    Just one problem I noticed.
    For PHP Versions without DOMXML, the exit(); on line 61 stops the rest of the page from loading.
    If you remove it everything works ok. I don’t think you need it anyway, since another conditional comes after it for PHP Versions with DOMXML.
    Please correct me if I’m wrong.

    Also, I was getting a “variable not set” warning for $found on line 59.
    This is not a problem, but I replaced the if($found) statement with if(isset($found)) to keep my logs clean.

    Thanks for the script Gerhard!!

  • yorgos

    sorry, I meant to thank Chris for the script… for some reason “Gerhard” got stuck after reading the last post.

  • tip

    You are invited to check out some helpful info about…

  • bert

    i´ve tried the great script (download the zip and install it) on 2 servers (Linux and win XAMMP)and it doesn´t work correctly on both servers. :-(
    I want to use the rsn-version , but i can see the sub-menues every time (the tree is correct and I didn´t change anything from the zip). On your server the example worked very well, but on my servers it doesn´t work fine(all in Firefox 1.5). Why? Can you please help me?
    Sorry for my bad english.

    best regards

    Here my phpinfo:
    DOM/XML enabled
    DOM/XML API Version 20031129
    libxml Version 2.6.19
    HTML Support enabled
    XPath Support enabled
    XPointer Support enabled
    Schema Support enabled
    RelaxNG Support enabled

  • notuo

    Great script!

    I finally mede it run properly and I have a couple of comments:

    1) Accented characters doesn’t work, You have to chage those to #char equivalents.
    2) I have a long (really long) links like:
    2002: Patrimonio intangible, Pachuca, Hidalgo

    So I change it to 2002 in order to reduce the footprint in the menu and not to loose the whole idea of the link. The title works fin, the link works fine but somehow after the click I lost the expanded part in this level.

    My guess is the comparison for the “active” link is made using the whole A tag intead of the HREF tag.

    Maybe a room for improvement.

    Notuo

  • notuo

    In regarding my earlier post Number 2. I originally put the title within the a after the href. The regexp is looking for the href at the end, so simply moving the title before the href I got what I want.

    Thanks!

  • notuo

    Here again. I have the script working in one site and started to use in another but the new one has php5 and domxml_tree function is not supported at all.

    Chris, Is there a way to update this script to php5 or have this domxml_tree function integrated into it.

    I think is really a shame to loose the script due this version issue.

    Regards,

    Notuo

  • notuo

    Hi all

    Regarding my question about php 5 here is a solution

    I found a page from Alexandre Alapetite (sorry but the links are disabled here). Any way you can google for “XML transition from PHP4 domxml to PHP5 dom”
    and you are set.

    I asked Alex for his help and he told me:


    From what I can see, the author uses domxml_xmltree() but does not use the “children” array attribute that is the only
    difference between domxml_xmltree() and domxml_open_mem().

    So, you can replace domxml_xmltree() by domxml_open_mem() and it should work. By the way, domxml_open_mem() is also faster and
    uses less memory.

    I have not tried myself, so please tell me if it works. If it works, you can possibly like to tell the author about this
    improvement.

    I will have a look anyway if I can add domxml_xmltree() to my library.

    So I did this changes, upload Alex library and works perfectly now in php5.

    Now you can have the chance to use this great software also with php5 using another great software.

    Thanks both, Chris and Alex for these solutions

  • David

    I have been working with the easynav script for a couple of weeks and have one dilemna that I still need to solve. Thanks to the commentors above I was able to get the script running on a PHP 5 box, but one issue remains. Is it possible to have the script work with links and pages that are in subdirectories. Such as a page that is mysite.com/subpage/ and then another at mysite.com/subpage/interior.php.

    I have tried it and the navigation defaults to a “none selected state”.

    Any thoughts.

  • http://www.awesome-art.org sam

    where can one get the library for the dom xml to php 5.0

    So, you can replace domxml_xmltree() by domxml_open_mem() and it should work. By the way, domxml_open_mem() is also faster and
    uses less memory.

    I have not tried myself, so please tell me if it works. If it works, you can possibly like to tell the author about this
    improvement.

    I will have a look anyway if I can add domxml_xmltree() to my library

  • http://www.onequickmemory.com onequickmemory

    I would like to say thanks to notuo. The comments from #12 were tested by me and worked on a site with shared hosting that discontinued support for PHP4 and is running a late version of PHP 5 now.

    From Christian’s Ch 7 example on dynamic navigation in Beginning JavaScript with DOM Scripting and Ajax: From Novice to Professional

    1. I obtained the code from :
    http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/index.en.html

    2. I appended the php code on the top line of the document globals.php and changed the document that loads from ‘test.xml’ to ‘navigation.php’ in my case.

    3. I then copied the library document “domxml-php4-to-php5.php” and uploaded it to the same directory as globals.php

    The script seems to be working great now where it was getting a Fatal Error: call to undefined function domxml_xmltree() previously.

    Thanks again.

  • http://www.onequickmemory.com onequickmemory

    BTW: I did use domxml_open_mem() instead of domxml_xmltree() on line 21 of globals.php

  • ale

    Hello,
    i really like this script, but i’m not php expert, and the script doesn’t seem to work on php5
    Anyone can post an uptaded version for php5??

    Thank you in advance!

blog comments powered by Disqus
Christian Heilmann's blog – Wait till I come! is the blog of Christian Heilmann , a developer evangelist living and working in London, England. Download vcard.

Feed me, Seymour: Entries (RSS) and Comments (RSS).