LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2004, 08:13 PM   #1
r_jensen11
Senior Member
 
Registered: Apr 2003
Location: Minnesota, USA
Distribution: Slack 10.0 w/2.4.26
Posts: 1,032

Rep: Reputation: 45
Problems with PHP menus for websites?


Okay, here's the coding for my pages, once I finish posting the 3 different pages, I'll ask my question:
nav.class.php
Code:
<?php

///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//  phpnav - a php class for displaying navigation items                     //
//  version 0.8 - 20010211                                                   //
//  copyright (c) 2002 - Paul Gareau <paul@xhawk.net>                        //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

//error_reporting(E_ALL);

class phpNav {

    /**
     * Object constructor
     */

    function phpNav($link_db='') {
        if($link_db) {
            $this->setLinks($link_db);
        }
		$this->_preset_vals = array();
    }

    /**
     * Set link array to be used
     */

    function setLinks($link_db) {

		$use_cache = 0; //using a cached db may be slower!
		$cache_file = 'cache/'.$link_db.'.cache';

        if(is_array($link_db)) {
            $this->_links = $link_db;
        }
        elseif($use_cache && file_exists($cache_file)) {
            $fp = fopen($cache_file, 'r');
            $this->_links = unserialize(fread($fp, filesize($cache_file)));
            fclose($fp);
        }
        elseif(file_exists($link_db)) {
            $file = file($link_db);
            foreach($file as $line) {
                if(ereg("^[^;#\r\n]", $line)) {
                    $this->_links[] = split("\t+", trim($line));
                }
            }
			if($use_cache) {
	            $fp = fopen($cache_file, 'w');
	            fwrite($fp, serialize($this->_links));
	            fclose($fp);
			}
        } else {
			echo 'no link data found';
            return false;
        }
    }

    /**
     * Pass extra properties to created objects
     */

    function preset($prop, $val='') {
        $this->_preset_vals[] = array($prop=>$val);
    }

    function _do_preset(&$obj) {
        if($this->_preset_vals) {
            foreach($this->_preset_vals as $array) {
                list($prop, $val) = each($array);
                $obj->$prop = $val;
            }
        }
    }

    /**
     * Create a new phpNav object
     */

    function &create($type, $arg1='', $arg2='') {
        if(!isset($this->_links)) {
			echo 'no link data!';
            return false;
        }
        $type	= strtolower($type);

        switch ($type) {
        case 'trail':
        case 'trailnav':
            $nav = new TrailNav($this->_links, $arg1);
            break;
        case 'link':
        case 'linknav':
            $nav = new LinkNav($this->_links, $arg1, $arg2);
            break;
        case 'tab':
        case 'tabnav':
            $nav = new TabNav($this->_links, $arg1, $arg2);
            break;
        case 'button':
        case 'buttonnav':
            $nav = new ButtonNav($this->_links, $arg1, $arg2);
            break;
        case 'dropdown':
        case 'dropdownnav':
            $nav = new DropdownNav($this->_links, $arg1);
            break;
        case 'tree':
        case 'treenav':
            $nav = new TreeNav($this->_links, $arg1, $arg2);
            break;
        default:
            echo 'Invalid Nav Type!';
        }
        $this->_do_preset($nav);
        return $nav;
    }

}

/*********************************************************************/

class commonNav {

    /**
     * Object constructor
     */

    function commonNav() {

        $this->_use_cache = 1;
        $this->_cache_dir = dirname(__FILE__).'/cache/';

        // set common property values
        $this->_c_selected_is_text	= 1;
        $this->_c_bgcolor		= '#CCCCCC';
        $this->_c_tab_color		= '#DDDDDD';
        $this->_c_sel_color		= '#FFFFEE';
        $this->_c_button_color	= '#EEEEEE';
        $this->_c_link_font		= array("<font face='verdana' color='black' size=2><b>", "</b></font>");

        // count the number of links in given array
        $this->_num_links = count($this->_links);
    }

    /**
     * Get the name of a page, given its path
     */

    function _getPageFromPath($path='') {
		//echo 'ran _getPageFromPath()';
        if(!$path) {
            global $PHP_SELF;
            $path = $PHP_SELF;
        }
        for($num=0; $num<$this->_num_links; $num++) {
            if($this->_links[$num][0] == $path) {
                return $this->_links[$num][1];
            }
        }
        return false;
    }

    /**
     * Get the parent of a page
     */

    function _getPageParent($page='') {
		//echo 'ran _getPageParent()';
        if(!$page) {
			if(isset($this->_page)) {
	            $page = $this->_page;
			} else {
				$page = $this->_getPageFromPath();
			}
        }
        for($num=0; $num<$this->_num_links; $num++) {
            if($this->_links[$num][1] == $page) {
                return $this->_links[$num][2];
            }
        }
        return false;
    }

    /**
     * Generate tabs or use cached version
     */

    function &html() {
        if($this->_use_cache) {
            $file_name = $this->_cache_dir.md5(serialize($this)).'.cache';
            if(file_exists($file_name)) {
                //echo 'using cached';
                $fp = fopen($file_name, 'r');
                $html = fread($fp, filesize($file_name));
                fclose($fp);
                return $html;
            } else {
                //echo 'generating';
                $html = &$this->generate();
                $fp = fopen($file_name, 'w');
                fwrite($fp, $html);
                fclose($fp);
                return $html;
            }
        } else {
            return $this->generate();
        }
    }

    function _trans($string) {
        //translation function
        //return trans($string);
        return $string;
    }
}

/*********************************************************************/

class TrailNav extends commonNav {
*Stuff cut out to make this more condense*
}

/*********************************************************************/

class LinkNav extends commonNav {
*Stuff cut out to make this more condense*
}

/*********************************************************************/

class TabNav extends commonNav {
*stuff cut out so this thread would be short enough to post*
}

/*********************************************************************/

class ButtonNav extends commonNav {
    function ButtonNav(&$links, $section='', $page='') {
        $this->_links = $links;
        $this->commonNav();

		if($page=='!') {
			$this->_page = '';
		} elseif($page) {
			$this->_page = $page;
		} else {
			$this->_page = $this->_getPageFromPath();
		}
        $this->_section = ($section) ? $section : $this->_getPageParent();

        $this->selected_is_text		= $this->_c_selected_is_text;
        $this->style				= 'vert'; //can be horiz or vert
        $this->text_padding			= '&nbsp;&nbsp;';
        $this->bgcolor				= $this->_c_bgcolor;
        $this->table_width			= '';
        $this->table_align			= '';
        $this->text_align			= 'left';

        $this->button_align			= 'left';
        $this->button_width			= 150;
        $this->button_space			= 0;
        $this->button_padding		= 2;
        $this->button_spacing		= 2;
        $this->button_font			= $this->_c_link_font;
        $this->open_button_font		= $this->_c_link_font;
        $this->button_color			= $this->_c_button_color;
        $this->open_button_color	= $this->_c_sel_color;
    }

    function &generate() {
		$found_links = 0;
        $html = "<table border=0 cellpadding=0 cellspacing=0 width='$this->table_width'";
        if($this->table_align) {
            $html.= " align='$this->table_align'";
        }
		//$html.= " style='border-style: outset; border-top-style: none; border-width: 1'";
        $html.= ">";
        $html.= "<tr><td bgcolor='$this->bgcolor'>\n";
        $html.= "<table border=0 cellpadding=$this->button_padding cellspacing=$this->button_spacing align='$this->button_align'";

        if($this->button_width=='-') {
            $html.= " width='100%'";
        }
        $html.= "><tr>\n";
        for($num=0; $num<$this->_num_links; $num++) {
            if($this->_links[$num][2]==$this->_section) {
                if($this->_links[$num][1]==$this->_page) {
                    $bgcolor = $this->open_button_color;
                    $font = $this->open_button_font[0];
                    $cfont = $this->open_button_font[1];
                } else {
                    $bgcolor = $this->button_color;
                    $font = $this->button_font[0];
                    $cfont = $this->button_font[1];
                }
                if($this->style=='vert' && $found_links) {
                    $html.='<tr>';
                }
                $html.= "<td bgcolor='$bgcolor' align='$this->text_align'";
                if($this->button_width && $this->button_width!='-') {
                    $html.= " width='$this->button_width'";
                }
				//$html.= " style='border-style: inset; border-width: 1'";
                $html.= ">";
                $html.= $font.$this->text_padding;
				$name = $this->_trans($this->_links[$num][1]);
				$name = ereg_replace('[[:space:]]', '&nbsp;', $name);
                if($this->selected_is_text && $this->_links[$num][1] == $this->_page) {
                    $html.= $font.$name.$cfont;
                } else {
                    $html.= "<a href='".$this->_links[$num][0]."' title='".$this->_links[$num][1]."'>";
                    $html.= $font.$name.$cfont;
                    $html.= '</a>';
                }
                $html.= $this->text_padding.$cfont."</td>\n";
                if($this->style=='vert') {
                    $html.='</tr>';
                }
                if($this->button_space) {
                    $html.= "<td width=$this->button_space></td>\n";
                }
                $found_links = true;
            }
        }
        if(! $found_links) {
            $html.= "<td height=1></td>\n";
        }
        if($this->style!='vert') {
            $html.= "</tr>\n";
        }
        $html.= "</table>\n";
        $html.= "</td></tr></table>\n";

        return $html;
    }
}

/*********************************************************************/

class DropdownNav extends commonNav {
    function DropdownNav(&$links, $level='') {
        $this->_links = $links;
        $this->commonNav();

        $this->_level = ($level) ? $level : $this->_getPageParent();

        $this->size		= 1;
        $this->text		= 'Quick Nav';
        $this->name		= 'quick_nav';
        $this->indent	= '·&nbsp;';
        $this->font		= $this->_c_link_font;
    }

    function &generate() {
        global $PHP_SELF;

        $html = "<form name='$this->name' action='$PHP_SELF' method='post'>\n";
        $html.= "<table border=0 cellpadding=0 cellspacing=0><tr>\n";
        $html.= "<td>\n";
        $html.= $this->font[0]."\n";
        $html.= "<select name='$this->name' size=$this->size";
        $html.= " onChange=\"if(this.value != '') document.location = this.value\"";
		$html.= " style='font-family: lucida console, courier'>\n";
        $html.= "<option value='' selected>$this->text\n";

		$opt = '';
		$max_len = 0;
        for($num=0; $num<$this->_num_links; $num++) {
            if($this->_links[$num][1]==$this->_level || ($this->_level=='' && $this->_links[$num][2]==$this->_level)) {
				$name = $this->_trans($this->_links[$num][1]);
                $opt.= "<option value='".$this->_links[$num][0]."'>".$name."\n";
                for($sub_num=0; $sub_num<$this->_num_links; $sub_num++) {
                    if($this->_links[$sub_num][2]==$this->_links[$num][1]) {
						$name = $this->_trans($this->_links[$sub_num][1]);
                        $opt.= "<option value='".$this->_links[$sub_num][0]."'>$this->indent".$name."\n";
						if(strlen($this->_links[$sub_num][1] > $max_len)) {
							$max_len = strlen($this->_links[$sub_num][1]);
						}
					}
                }
            }
        }
		$spc = str_repeat('-', $max_len+strlen($this->indent));
        $html.= "<option value=''>$spc\n";
		$html.= $opt;
		$html.= "</select>\n";
        $html.= $this->font[1]."\n";
        $html.= "</td>\n";
        $html.= "</tr></table>\n";
        $html.= "</form>\n";

        return $html;
    }
}

/*********************************************************************/

class TreeNav extends commonNav {
    function TreeNav(&$links, $level='', $page='') {
        $this->_links = $links;
        $this->commonNav();

        $this->_level = ($level) ? $level : $this->_getPageParent();
        
		if($page=='!') {
			$this->_page = '';
		} elseif($page) {
			$this->_page = $page;
		} else {
			$this->_page = $this->_getPageFromPath();
		}
        $this->max_depth = 2;
        $this->link_font = $this->_c_link_font;
        $this->selected_is_text		= $this->_c_selected_is_text;

		$this->_depth = 0;
		$this->_level_hold = 0;
    }

    function &generate() {
        if($this->_depth > $this->max_depth) return false;
        $level = ($this->_level_hold) ? $this->_level_hold : $this->_level;
		$html = '';
        for($num=0; $num<$this->_num_links; $num++) {
            if($this->_links[$num][2]==$level && $this->selected_is_text) {
                $html.= "<li>";
				$name = $this->_trans($this->_links[$num][1]);
				if($this->_links[$num][1]==$this->_page) {
					$html.= $this->link_font[0].$name.$this->link_font[1];
				} else {
					$html.= "<a href='".$this->_links[$num][0]."'>";
	                $html.= $this->link_font[0].$name.$this->link_font[1]."</a>";
				}
                $this->_depth++;
                $this->_level_hold = $this->_links[$num][1];
                $html.= $this->generate();
                $this->_depth--;
            }
        }
        if($html) {
			return "<ul>\n$html</ul>\n";
		}
    }
}

?>
frame.php
Code:
<HTML><HEAD><TITLE>Bloomington United for Youth</TITLE></HEAD>
<BODY BGCOLOR="FFFFFF">
<TABLE WIDTH="100%" BGCOLOR="FEFFB2" BOARDER="0">
<TR><TD COLSPAN="2" >
<CENTER><H1>Bloomington United for Youth</H1></CENTER>
</TD></TR>
<TR><TD BGCOLOR="95C576" WIDTH="150">
<P><CENTER><H3>Menu</H3></CENTER>
<?
// include phpnav class
include 'nav.class.php';

// create phpnav object for given database
$php_nav = new phpNav('link_db.txt');

$dropdown =& $php_nav->create('dropdown', 'Youth');

// create a 'trail' object for page 'Articles'
$button =& $php_nav->create('button', 'Home');

// print trail object html
print $button->html();
echo '<HR>';
print $dropdown->html();
?>
<hr>
</P>
</TD>
<TD BGCOLOR="FFFFFF">
<P ALIGN="JUSTIFY"><H2 ALIGN="CENTER">Content</H2>
&nbsp; This is where the content will be for standard pages.  This is where the
content will be for standard pages.  This is where the content will be for
standard pages.  This is where the content will be for standard pages.  
</P>
</TD>
</TR>
<TR><TD COLSPAN="2" BGCOLOR="91CAFF">
&copy;2004 Bloomington United for Youth
</TD></TR>
</TABLE>
</BODY>
</HTML>
link_db.txt
Code:
#
# PHP Nav Sample Database
#
# blank lines and commented lines are ignored
# comments are ; and #
# fields are: path	page_name	parent_name
# path and page name are (should be) primary keys
# fields are delimited by one or more tabs

# Switch links
/index.php	Index	Home
/frame.php	Frame	Home
/index-youth.php	Youth		Home
/index-cm.php	Commitment Members		Home
/index-bm.php	Board Members		Home

# Alpha links
/index.html		Splash					Youth
/index-youth.php	Youth and Adults	Youth
/index-cm.php	Commitment Members	Youth
/index-bm.php	Board Members			Youth

# Top level links
/index-cm.php	Home		CM
/index.php	Classifieds	CM
/index.php	Events		CM
/index.php	Groups		CM
/index.php	Forums		CM
;/index.php	Places		CM

# Article sub-links
/index.php	Section One	Board
/index.php	Section Two	Board

# Switch links
Okay, now what I want is for the dropdown menu to use a group of URLs labeled Switch. However, I'm unable to have the dropdown work unless I have the group be labeled Home, or Youth. I've tried many other names, but none have worked.

Also, would it be possible at all to have this menu appear in an .html file? When I tried, it didn't display, and I've only gotten it to work in .php files.

Last edited by r_jensen11; 02-08-2004 at 10:22 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with Vhosts.conf file and websites copatlarge Linux - Newbie 3 09-25-2005 04:02 PM
Internet problems on LAN, cant ping certain websites, help please lurker Linux - Networking 2 07-17-2004 10:29 AM
Problems connecting to regular gnome.org websites. ParsErr Linux - General 0 09-27-2003 08:56 PM
repaint problems with pop-ups, menus linux_inst Linux - Distributions 0 08-26-2003 06:54 PM
fluxbox menus - problems rlculver Linux - Software 4 11-27-2002 09:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration