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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Building an Extensible Menu Class
By: Team Melonfire, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek