$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";
// 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 = " ";
// 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);
// 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);
// 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";
// 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
}
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{
// 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";