PHP
  Home arrow PHP arrow Page 6 - Building an Extensible Menu Class
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building an Extensible Menu Class
By: Team Melonfire, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2001-08-10

    Table of Contents:
  • Building an Extensible Menu Class
  • Back To Class
  • What's On The Menu?
  • Children And Their Parents
  • I Say Method, You Say Madness...
  • Rounding Up The Family
  • Saving My Bookmarks
  • Reaching Higher
  • Collapsing Inwards
  • Extending Yourself

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building an Extensible Menu Class - Rounding Up The Family


    (Page 6 of 10 )

    Next, one of the most useful methods in this collection - the get_children() method. This method accepts a node id and returns an array containing the next level of the menu tree.

    <? class Menu { // other methods // function: get next level of menu tree // returns: array function get_children($id) { $query = "SELECT id, label, link FROM $this->table WHERE parent = '$id'"; $result = $this->query($query); $count = 0; while ($row = mysql_fetch_array($result)) { $children[$count]["id"] = $row["id"]; $children[$count]["label"] = $row["label"]; $children[$count]["link"] = $row["link"]; $count++; } return $children; } } ?>
    This method accepts a node id and queries the database for a list of items which reference that node in the "parent" column. These records are packaged as an array of arrays and returned to the calling function. Here's an example of how it could be used:

    <? $obj = new Menu(); // get children $arr = $obj->get_children(1); echo "<ul>"; // iterate through array for ($x=0; $x<sizeof($arr); $x++) { echo "<li>" . $arr[$x]["label"]; } echo "</ul>"; ?>
    And this correctly displays the children of node id 1 (USA) to be

    California Massachusetts
    A useful corollary of this is the get_type() method, which can be 
    used to identify whether a particular node on the tree has children or not - in other words, whether it is a leaf or a branch. 

    <? class Menu { // other methods // function: test whether this id is a branch or leaf // returns: boolean function get_type($id) { if($this->get_children($id) ) { return 1; } else { return 0; } } } ?>
    The get_ancestors() method does the reverse of the get_children() method - it returns a list of nodes between the tree root and the supplied node identifier, starting from the top of the menu tree and proceeding downwards.

    <? class Menu { // other methods // function: return a list of this node's parents // by travelling upwards all the way to the root of the tree // returns: array function get_ancestors($id, $count = 0) { // get parent of this node $parent = $this->get_parent($id); // if not at the root, add to $ancestors[] array if($parent) { $this->ancestors[$count]["id"] = $parent; $this->ancestors[$count]["label"] = $this->get_label($parent); $this->ancestors[$count]["link"] = $this->get_link($parent); // recurse to get the parent of this parent $this->get_ancestors($this->ancestors[$count]["id"], $count+1); // all done? at this stage the array contains a list in bottom-up order // reverse the array and return return array_reverse($this->ancestors); } } } ?>
    Returning to the example above, an attempt to find out the ancestors of node id 5 (Boston)

    <? $obj = new Menu(); // get children $arr = $obj->get_ancestors(5); echo "<ul>"; // iterate through array for ($x=0; $x<sizeof($arr); $x++) { echo "<li>" . $arr[$x]["label"]; } echo "</ul>"; ?>
    would return

    USA Massachusetts
    Finally, the print_menu_tree() method comes in handy while debugging, 
    if you need to visually see the complete menu tree with its internal dependencies. 

    <? class Menu { // other methods // function: display complete menu tree (useful when debugging) // returns: HTML list function print_menu_tree($id = 0) { $result = $this->get_children($id); echo "<ul>"; for ($x=0; $x<sizeof($result); $x++) { echo "<li>" . $result[$x]["label"] . "[" . $result[$x]["id"] . "]"; $this->print_menu_tree($result[$x]["id"]); } echo "</ul>"; } } ?>

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


     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT