Home arrow PHP arrow Page 9 - Building an Extensible Menu Class

Collapsing Inwards - PHP

So you know the theory behind OOP, but don't really understandits applications? Well, it's time to take objects out of the classroom andinto the real world - this article demonstrates how OOP can save you timeand effort by building a PHP-based Menu object to describe therelationships in a hierarchical menu tree. And since the proof of thepudding is in the eating, it then combines the newly-minted Menu objectwith some of the most popular JavaScript menu systems available online toshow you how cool objects really are.

TABLE OF CONTENTS:
  1. Building an Extensible Menu Class
  2. Back To Class
  3. What's On The Menu?
  4. Children And Their Parents
  5. I Say Method, You Say Madness...
  6. Rounding Up The Family
  7. Saving My Bookmarks
  8. Reaching Higher
  9. Collapsing Inwards
  10. Extending Yourself
By: Team Melonfire, (c) Melonfire
Rating: starstarstarstarstar / 6
August 10, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
If you go back a few pages, you'll notice that one of the design goals of this Menu object was to separate the visual presentation of a menu from the relationships between the various nodes. I've already demonstrated how the same Menu object can be used to create a directory and a hierarchical menu tree with standard methods.

My third example is similar to the second, again connecting a JavaScript-based menu system to the menu database to dynamically build a menu tree. For this, I plan to use another very popular menu constructor, FolderTree (free version available at http://www.geocities.com/marcelino_martins/foldertree.html ), which uses Windows Explorer-style files and folders to display a hierarchy of items.

First, I need an HTML page to invoke FolderTree and display the menu:

<html> <head> <basefont face="Arial"> <link rel="stylesheet" href="ftie4style.css"> <!-- Infrastructure code for the tree --> <script src="ftiens4.js"></script> <!-- Execution of the code that actually builds the specific tree --> <script src="menu.js.php"></script> <script> initializeDocument() </script> </head> <body bgcolor=white> </body> </html>
As you can see, this file sources "menu.js.php", which contains the actual menu data. This file is usually created manually as per the user's requirements; I plan to hook it up to my database table to generate it dynamically. Here's the code:

// menu.js.php // set up tree root foldersTree = gFld("Melonfire", "http://www.melonfire.com"); <? // initialize object include("menu.class.php"); $obj = new Menu(); // counter - used to create tree nodes $count = 0; function buildMenu($id, $node) { global $obj; global $count; // get children $children = $obj->get_children($id); for ($x=0; $x<sizeof($children); $x++) { // write the name of this node $newNode = "aux" . $count; // if folder if($obj->get_type($children[$x]["id"]) == 1) { // call appropriate FolderTree function to display folder icon $count++; $str = $newNode . " = insFld(" . $node . ", gFld(\"" . $children[$x]['label'] . "\", \"" . $children[$x]['link'] . "\"));"; print $str; // and recurse for next level buildMenu($children[$x]["id"], $newNode); } else { // if last level // call appropriate FolderTree function to display file icon $str = "insDoc(" . $node . ", gLnk(2, \"" . $children[$x]['label'] . "\", \"" . $children[$x]['link'] . "\"));"; print $str; } } } // initiate recursive function buildMenu(0, "foldersTree"); ?>
You just gotta love those recursive functions!

Here's what it all looks like:



 
 
>>> More PHP Articles          >>> More By Team Melonfire, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: