WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Unlimited PHP Multi-Level Menu(Stinky Menu)
Categories : PHP, PHP Classes
blacksnday i-am-mean.com
Date : Jul 19th 2008
Grade : 3 of 5 (graded 10 times)
Viewed : 14683
File : 4830.zip
Images : Image 1
Search : More code by blacksnday i-am-mean.com
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
Like this code?
Show the author your appreciation.
 

Please config the MySQL DB setup in the index file.

Please read below for full example and instructions.
Also included is a Screenshot of the example below.

Part One is example usage with all setup options available
Part Two is the Class itself

<style type="text/css">

ul {list-style:none;}
img {border:none;}

.stinky {
font-size: 0.9em;
        text-decoration: none;
        color: #000000;
        margin-left: 5px;
}

.stinky1 a {
         color: red; text-decoration: none;
         font-size: 1.1em;
        }
</style>

<?php


/*
MYSQL DATABASE DUMP WITH EXAMPLE MENU ITEMS AND IMAGES:

CREATE TABLE IF NOT EXISTS `stinky_menu` (
  `id` int(5) NOT NULL auto_increment,
  `title` varchar(120) NOT NULL default '',
  `active` tinyint(1) NOT NULL default '0',
  `nest_under` int(5) NOT NULL default '0',
  `description` varchar(255) NOT NULL,
  `url` varchar(255) NOT NULL,
  `img` varchar(255) NOT NULL,
  UNIQUE KEY `id` (`id`),
  KEY `nest_under` (`nest_under`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;

--
-- Dumping data for table `stinky_menu`
--

INSERT INTO `stinky_menu` (`id`, `title`, `active`, `nest_under`, `description`, `url`, `img`) VALUES
(8, 'Sub Menu Item 3', 1, 6, 'Games d0t com!', 'http://games.com', '0194_diamond.png'),
(6, 'Menu Item 2', 1, 0, 'SHORT URL within website', 'short_url', ''),
(1, 'Menu Item 1', 1, 0, 'I am mean d0t com', 'http://i-am-mean.com', '0195_heart.png'),
(2, 'Sub Menu Item 1', 1, 1, 'Google . com', 'HTTP://google.com', '0196_clubs.png'),
(7, 'Sub Menu Item 2', 1, 6, 'Yahoo!!!!', 'http://yahoo.com', ''),
(3, 'Sub Menu Item 1.1', 1, 2, 'I LOVE CARS!', 'cars', '0200_small_arrow_in_box.png'),
(4, 'Sub Sub Menu Item 1.3', 1, 3, 'Weber Dev', 'http://weberdev.com', ''),
(5, 'Sub Sub Menu Item 1.2', 1, 3, 'Blacksnday d0t com', 'http://blacksnday.com', '');
   

*/

   
$dbHost   "localhost";
   
$dbUser   "db-user";
   
$dbPass   "db-pass";
   
$dbName   "db-name";
   
   
$link=mysql_connect("$dbHost","$dbUser","$dbPass");
    if(!
$link){exit(mysql_error());}
   
mysql_select_db("$dbName",$link) or exit(mysql_error());
   
   
   
//initiate the class and tell it what your MySQL table name is for the menu table
       
$menu = new Multi_Level_Menu($sql_table_name="stinky_menu");

   
// Sets the Base URL for which the menu will create links from.
    // THIS IS OPTIONAL! you can include full urls in the Menu SQL Database.
    // If you do mix FULL URLS and SHORT URLS DONT WORRY! FULL and SHORT URLS are automatically detected!(applaud!)
    // However, if you do use SHORT URLS this should be set.
    // THIS MUST be called before display_menu()!!
       
$menu->menu_base_url("?goto=");
       
   
// Choose wether or not you want the menu item to open in a new window (default=FALSE)
    // This will only work for FULL URLS. Short URLS will always open in same window
    // THIS MUST be called before display_menu()!!
       
$menu->menu_href_new_window($new_window=TRUE);
       
   
// The Below Two Options are for how the Sub-Level Menu Items are displayed
    // THIS MUST be called before display_menu()!!
       
$menu->menu_sub_start_tag("<span class='stinky'> -> ");
       
$menu->menu_sub_end_tag("</span>");
   
   
// Chose wether or not(TRUE or FALSE) you wish for images to be displayed within the left of link names
    // If TRUE, it uses the images uploaded to the below folder name, and placed within the img sql field
    // DOES NOT WORK WITH THE display_select_menu() option!
    // THIS MUST be called before display_menu()!!
       
$menu->use_menu_img(TRUE);
       
   
// Name of the folder being used to store the images
    // THIS MUST be called before display_menu()!!
       
$menu->menu_img_folder($folder="stinky_imgs");
       
echo
"<h1>STINKY MENU</h1>";

echo
"Menu in List Format<br />";
echo
"<ul>";
   
// The Below displays the menu, as well as provides the starting and ending of the formatting for Top-Level Menu Items
       
$menu->display_menu($format_menu_start="<li class=\"stinky1\">", $format_menu_end="</li>");
echo
"</ul>";

echo
"<br /><br />";



echo
"Menu in Select-List Format<br />";
   
   
// The Below displays the menu, as well as provides the starting and ending of the formatting for Top-Level Menu Items
        // !!IMPORTANT!! $select_name MUST be the same as $menu->menu_base_url()
            //EXAMPLE: $menu->menu_base_url("?goto=")  $select_name="goto"
       
$menu->display_select_menu($select_top_level_name="- SELECT -", $form_name="menu_bar", $action="", $select_name="goto", $option_name="menu_id");
?>


<?php

class Multi_Level_Menu
{

function
Multi_Level_Menu($sql_table_name)
{
   
   
$this->table_name = $sql_table_name;   
   
}
function
build_menu($current_cat_id, $count)
{

    static
$option_results;
   
   
// if there is no current category id set, start off at the top level (zero)
       
if (!isset($current_cat_id))
        {
           
           
$current_cat_id =0;

        }
       
   
// increment the counter by 1
       
$count = $count+1;

   
// query the database for the sub-categories of whatever the parent category is
       
$sql = "SELECT id, title FROM ".$this->table_name." WHERE nest_under = '$current_cat_id' AND active='1' ORDER BY title ASC";
       
       
$get_menu = mysql_query($sql);
       
$num_options = mysql_num_rows($get_menu);
       
   
// our category is apparently valid, so go ahead…
       
if ($num_options > 0)
        {
           
            while (list(
$cat_id, $cat_name) = mysql_fetch_row($get_menu))
            {
           
// if its not a top-level category, indent it to show that its a child category
               
if ($current_cat_id!=0)
                {
                   
$indent_flag = " ";
       
                    for (
$x=2; $x<=$count; $x++)
                    {
                       
$indent_flag .= $this->display_start_tag;
                       
$indent_flag_end .= $this->display_end_tag;
                       
                    }
                }
       
               
$cat_name = $indent_flag.$cat_name.$indent_flag_end;
               
$option_results[$cat_id] = $cat_name;
       
       
           
// now call the function again, to recurse through the child categories
               
$this->build_menu($cat_id, $count);
            }
           
        }
       
mysql_free_result($get_menu);
       
        return
$option_results;
       
}

// Creates the opening style tag for SUB-LEVEL MENU Items placed within build_menu()
function menu_sub_start_tag($start_tag="")
{
    if(
$start_tag == FALSE){$this->display_start_tag = " ->";}else{$this->display_start_tag = $start_tag;}
   
    return
$this->display_start_tag;
       
}

// Creates the closing style tag placed within build_menu()
function menu_sub_end_tag($end_tag="")
{
    if(
$end_tag == FALSE){$this->display_end_tag = "";}else{$this->display_end_tag = $end_tag;}
   
    return
$this->display_end_tag;
       
}

// Grabs the ID for the selected menu item
function get_link_id($id)
{
   
       
$sql = "SELECT id FROM ".$this->table_name." WHERE id = '".$id."'";
       
$result = mysql_query($sql);   
       
$row = mysql_fetch_row($result);
       
        return
$row[0];
       
       
mysql_free_result($result);
   
}

// Grabs the Title for the selected menu item
function get_link_title($id)
{
   
       
$sql = "SELECT title FROM ".$this->table_name." WHERE id = '$id'";
       
$result = mysql_query($sql);   
       
$row = mysql_fetch_row($result);
       
        return
$row[0];
       
       
mysql_free_result($result);   
   
}

// Grabs the Description(which is placed in the TITLE tag) for the selected menu item
function get_link_description($id)
{
   
       
$sql = "SELECT description FROM ".$this->table_name." WHERE id = '$id'";
       
$result = mysql_query($sql);   
       
$row = mysql_fetch_row($result);
       
        return
$row[0];
       
       
mysql_free_result($result);
}

// Grabs the URL for the selected menu item
function get_link_url($id)
{
   
       
$sql = "SELECT url FROM ".$this->table_name." WHERE id = '$id'";
       
$result = mysql_query($sql);   
       
$row = mysql_fetch_row($result);
       
        return
$row[0];
       
       
mysql_free_result($result);
       
}

function
use_menu_img($default=FALSE)
{
   
    if(
$default == FALSE){$this->use_link_imgs = FALSE;}else{$this->use_link_imgs = TRUE;}
   
    return
$this->use_link_imgs;
}

function
menu_img_folder($folder="")
{
   
    if(
$folder == FALSE){$this->img_folder = "imgs";}else{$this->img_folder = $folder;}
   
    return
$this->img_folder;
   
}

// Grabs the IMG(if there is one) for the selected menu item
function get_link_img($id)
{
    if(
$this->use_link_imgs == TRUE)
    {
       
$sql = "SELECT img FROM ".$this->table_name." WHERE id = '$id'";
       
$result = mysql_query($sql);   
       
$row = mysql_fetch_row($result);
       
        return
$row[0];
       
       
mysql_free_result($result);
    }else{
        return
FALSE;
    }
}

function
menu_base_url($url)
{
   
   
$this->url = $url;
   
    return
$this->url;
       
}

function
menu_href_new_window($new_window=FALSE)
{
    if(
$new_window == FALSE){$this->open_new_window = FALSE;}else{$this->open_new_window = " target=\"_new\"";}
   
    return
$this->open_new_window;
       
}

function
display_menu($format_menu_start, $format_menu_end)
{
   
   
$get_menu = $this->build_menu(0, 0);

    if (
count($get_menu) > 0)
    {

        foreach (
$get_menu as $key => $value)
        {
           
            if(
$this->get_link_img($key) == TRUE)
            {
             
$url_img = "<img src=\"".$this->img_folder."/".$this->get_link_img($key)."\"></img>";
              }else{
             
$url_img = FALSE;
            }
           
           
// determine wether or not the url for the item is a full url or not.
            // if it is a full url, we change the menu output to reflect it
               
$string = "http://";
               
$container = $this->get_link_url($key);

                if(
stristr($container,$string))
                {
               
//here we build the style and the url output
                   
$options .="$format_menu_start<a href=\"".$this->get_link_url($key)."\" title=\"".$this->get_link_description($key)."\"".$this->open_new_window.">{$url_img}{$value}</a>$format_menu_end\n";
           
                }else{

                   
$options .="$format_menu_start<a href=\"".$this->url.$this->get_link_url($key)."\" title=\"".$this->get_link_description($key)."\">{$url_img}{$value}</a>$format_menu_end\n";
       
                }

        }
    }
   
echo
$options;

}

// display_select_menu does exactly as it says.....
// shows the menu as a select menu
// if you dont like the normal display_menu above then you can use this option
function display_select_menu($select_top_level_name="", $form_name="", $action="", $select_name="", $option_name="")
{
   
// The below javascript will do three things:
//
// 1. Auto-Forward the user to the menu link selected
//
// 2. Check if you have set for menu items to open in a new window
//
// 3. Check to see if the selected menu item is either a Full or Short URL
//
// If you have set for menu items to open in a new window, and if the current menu item
// is a Full URL, the javascript will automatically open in a new window when the menu item is selected
?>
    <script language="JavaScript">
       
        function gotocluster(s){       
            var d = s.options[s.selectedIndex].value.toLowerCase();
            var basestring="window"+new Date().getTime();

            var <?php if($this->open_new_window == TRUE){?>
                new_window=true
                <?php }else{ ?>
                new_window=false
                <?php } ?>
            var match_url = d.match("http");
           
            if(d){
                if(new_window && match_url){
               
                        window.open(d,"basestring")
                   
                 }else{
                    parent.location.href = d
                 }
            }
            s.selectedIndex=0
        }
       
    </script>
<?php
   
echo "<form name='$form_name' method='post' action='$action'>
      <select name=\"
$select_name\" onChange=\"gotocluster(this)\">
      <option name=\"
$option_name\" value=\"\">$select_top_level_name</option>";


$get_menu = $this->build_menu(0, 0);

if (
count($get_menu) > 0)
{
   
$on_option = $_GET[$select_name];

    foreach (
$get_menu as $key => $value)     
    {

       
// determine wether or not the url for the item is a full url or not.
        // if it is a full url, we change the menu output to reflect it
           
$string = "http://";
           
$container = $this->get_link_url($key);

            if(
stristr($container,$string))
            {
           
//here we build the the url output
               
$options .="<option name=\"$option_name\" value=\"".$this->get_link_url($key)."\"";
               
$this->full_url = TRUE;
            }else{
           
               
$options .="<option name=\"$option_name\" value=\"".$this->url.$this->get_link_url($key)."\"";
       
            }
           
       
// here we determine if the current selection is the current page.
        // if so, lets keep it showing in the select box
           
if ($_GET[$select_name] == $this->get_link_url($key))
            {
               
$options .=" selected=\"{$pre_url_string}$on_option\"";
            }
       
       
//finally we close up shop
           
$options .=">$value</option>\n";
   
    }
}

echo
$options;
echo
"</select>
</form>"
;
   
}

}
?>



How To Create a PDF Using PHP
Categories : PHP, PDF, PHP Classes, HTML and PHP
PHP Composer - This class is meant to render images of the musical score of ring tones notes used in cellular phones, defined in the RTTL format.
Categories : PHP, PHP Classes, Misc
Banknote Validation - A PHP class that provides several methods to quickly validate banknote serial numbers of the following currencies: AUD, CAD, CHF, CNY, EUR, GBP, JPY, USD.
Categories : PHP, PHP Classes, Data Validation, Regexps
Scramble Eggs - php class to scramble/encode
Categories : PHP, PHP Classes, Security, Encryption
PDF class - This is a useful class to make a pdf file with php functions.
Categories : PHP, PDF, PHP Classes
Image Generation Class ( PNG Format )
Categories : PHP, GD image library, PHP Classes, Graphics
Directory Listing To XML : Outputs XML File of a Given Directory Listing
Categories : PHP, PHP Classes, XML, Directories
Class to build a select tag in html, useful to build select boxes from a data base
Categories : PHP, HTML and PHP, PHP Classes
php for odbc /* connect to access from odbc */
Categories : PHP Classes, ODBC, MS Access, PHP
XML easy parser
Categories : PHP, XML, PHP Classes
Automatic Browsers Detect
Categories : PHP, PHP Classes, Headers, Browsers
A class to put get and post variables in hidden form elements. Works on scalars, normal arrays, associative arrays.
Categories : Algorithms, Variables, Arrays, PHP, PHP Classes
These PHP Classes Check if a host is alive using various methods.
Categories : PHP, PHP Classes, Sockets, CURL
[PHP5] PHP Debugger and Helper
Categories : PHP, PHP Classes, Errors and Logging, Debugging, XML
EasyPhpThumbnail Class - The EasyPhpThumbnail class allows you to generate thumbnails and handle image manipulation for GIF, JPG and PNG on-the-fly.
Categories : PHP, PHP Classes, Object Oriented, Graphics, GD image library