PHP
  Home arrow PHP arrow Page 5 - 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 - I Say Method, You Say Madness...


    (Page 5 of 10 )

    With the database design out of the way, it's now time to begin work on the methods which will interact with the records in the database. Before actually sitting down to code the class, it's a good idea to spend some time listing the important methods, together with their purpose. Here's my initial cut:

    query($query) - execute an SQL query;

    get_children($node) - return a collection of this node's children;

    get_parent($node) - return the identifier of this node's parent;

    get_label($node) - return the name of this node

    get_type($node) - return the node type (leaf or branch)

    is_root_node($node) - is this node at the root level or not?

    These are the essential methods; there may be more, which I will add as development progresses.

    Let's now begin by setting up the class definition:

    <? // menu.class.php // methods to obtain node and tree relationships class Menu { } ?><hr noshade size=1 color=#cccccc></pre></blockquote> Now, since this class will be talking to a database, I need to add a few variables to hold database-specific information. <blockquote><pre><hr noshade size=1 color=#cccccc><? class Menu { // set up some variables with default values // to hold database parameters // hostname, user and password var $hostname; var $user; var $pass; // database and table containing menu data var $db; var $table; } ?>
    PHP makes it possible to automatically execute a specific function when a new instance of a class is spawned. This function is referred to as a "constructor" and must have the same name as the class.

    In this case, I plan to initialize my Menu object with certain default values for the various database parameters. I have the option of assigning these variable-value pairs individually, or writing a method to assign them all in one fell swoop; I pick the latter.

    <? class Menu { // set up some variables // snip // constructor function Menu() { $this->set_database_parameters("localhost", "me", "bs49h5634", "apps", "menu"); } // function: set database parameters // returns: none function set_database_parameters($hostname, $user, $password, $db, $table) { $this->hostname = $hostname; $this->user = $user; $this->password = $password; $this->db = $db; $this->table = $table; } } ?>
    In case you're wondering, the $this prefix provides a convenient way to access variables and functions which are "local" to the class

    Now, all the methods listed above will be querying the database for information. Since the connect-and-query code is common to all of them, I can extract it into a separate method named query().

    <? class Menu { // other methods // function: execute query $query // returns: result identifier function query($query) { // connect $connection = mysql_connect($this->hostname, $this->user, $this->pass) or die ("Cannot connect to database"); // run query $ret = mysql_db_query($this->db, $query, $connection) or die ("Error in query: $query"); // return result identifier return $ret; } } ?>
    All my object methods can now simply use query() to execute SQL queries on the database. Further - if I ever decide to move to another database, I need only update the code in this single function to ensure that my class will continue to work with the new system.{mospagebreak title=Playing With Nodes} With the underlying, database-specific layer in place, I can now begin work on the main object methods. The first of these is also one of the simplest - it accepts a node id and returns the id of the node's parent.

    <? class Menu { // other methods // function: get parent // returns: node id function get_parent($id) { $query = "SELECT parent FROM $this->table WHERE id = '$id'"; $result = $this->query($query); $row = mysql_fetch_row($result); return $row[0]; } } ?>
    An offshoot of this is the is_root_node() method, used to test whether a particular node is at the base of the menu tree.

    <? class Menu { // other methods // function: is this node at the root of the tree? // returns: boolean function is_root_node($id) { if($this->get_parent($id) == 0) { return 1; } else { return 0; } } } ?>
    The get_label() and get_link() methods accept a node id and return the corresponding text and URL from the table.

    <? class Menu { // other methods // function: get label for $id // returns: string function get_label($id) { $query = "SELECT label FROM $this->table WHERE id = '$id'"; $result = $this->query($query); $row = mysql_fetch_row($result); return $row[0]; } // function: get link for $id // returns: string function get_link($id) { $query = "SELECT link FROM $this->table WHERE id = '$id'"; $result = $this->query($query); $row = mysql_fetch_row($result); return $row[0]; } } ?>
    Using the menu table constructed a few pages ago, the code

    <? $obj = new Menu(); echo $obj->get_label(3); ?>
    would return

    Los Angeles

    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 4 hosted by Hostway
    Stay green...Green IT