Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-25-2006, 04:30 PM   PM User | #1
Simply
New Coder

 
Join Date: Feb 2006
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Simply is an unknown quantity at this point
Menu Class - Automatic Menus Using PHP

menuWerx is a PHP class to generate a menu for your site. Options include: debug mode, horizontal or vertical orientation & style hooks, optional inline styles, and strict or transitional modes. See an example at: http://www.Clear-Mind.com/menuwerx/

//*********************************************************************************************
menuwerx.php: (example of implementation)
PHP Code:
<?php
        
include('menuwerx.class.php');
        
$menu = new menuWerx("index.php""hori");
?>
//*********************************************************************************************
menuwerx.config.php:
PHP Code:
<?php
        
/*
        *    menuWerx - Menu creation class for PHP
        *    version:     1.0a
        *    File:        menuwerx.config.php
        *    Liscense:    GNU - http://www.gnu.org/copyleft/gpl.html
        *    Author:        Heath Nail
        *    Website:    http://www.Clear-Mind.com
        *    Email:        heathnail@hotmail.com
        */
        // Configuration settings

        
$this->aSettings['debug']     = false;            // true to debug "UGLY!!", false for production sites
        
$this->aSettings['orientation'] = 'horizontal';        // 'horizontal' or 'vertical' CSS can be edited below
        
$this->aSettings['useInlineStyle'] ='yes';        // 'yes' turns on inline styles, 'no' disables inline styles
        // 'strict' disables use of target attribute
        // 'transitional' enables use of target attribute
        
$this->aSettings['mode'] = 'strict';

        
// Menu details.
        // First page.  The first or leftmost menu item should be page[1]!

        
$this->pages[1]['link']  = "menuwerx.php";            // filename
        
$this->pages[1]['name']  = "Test Page 1";            // Name displayed on the menu
        
$this->pages[1]['title'] = "The First Test Page";        // Title / Description of the link
        
$this->pages[1]['target']     = "_top";                // _top opens in current window _blank opens in new window
        // for more info on target goto http://www.w3schools.com/tags/tag_a.asp
        // Second page

        
$this->pages[2]['link']  = "menuwerx2.php";
        
$this->pages[2]['name']  = "Test Page 2";
        
$this->pages[2]['title'] = "The Second Test Page";
        
$this->pages[2]['target']     = "_top";

        
// Third page

        
$this->pages[3]['link']  = "menuwerx3.php";
        
$this->pages[3]['name']  = "Test Page 3";
        
$this->pages[3]['title'] = "The Third Test Page";
        
$this->pages[3]['target']     = "_top";

        
// Fourth page
        /*
        $this->pages[4]['link']  = "index4.php";
        $this->pages[4]['name']  = "Test Page 4";
        $this->pages[4]['title'] = "The Fourth Test Page";
        $this->pages[4]['target']     = "_top";
        */
        // Fifth page
        /*
        $this->pages[5]['link']  = "index5.php";
        $this->pages[5]['name']  = "Test Page 5";
        $this->pages[5]['title'] = "The Fifth Test Page";
        $this->pages[5]['target']     = "_top";
        */

        // 'vertical' css check out listamatic for cool ways to
        // style your lists http://css.maxdesign.com.au/listamatic/vertical08.htm
        
$this->css['vertical'] = "
        <style>
        #menuWerx { width: 200px; }

        #menuWerx ul
        {
        margin-left: 0;
        padding-left: 0;
        list-style-type: none;
        font-family: Arial, Helvetica, sans-serif;
}

        #menuWerx a
        {
        display: block;
        padding: 3px;
        width: 160px;
        background-color: #036;
        border-bottom: 1px solid #eee;
}

        #menuWerx a:link, #menuWerx-list a:visited
        {
        color: #EEE;
        text-decoration: none;
}

        #menuWerx a:hover
        {
        background-color: #369;
        color: #fff;
}

        #active a
        {
        background-color: #369;
}
        </style>
        "
;

        
// 'horizontal' css check out listamatic for cool ways to
        // style your lists http://css.maxdesign.com.au/listamatic/horizontal03.htm
        
$this->css['horizontal'] = "
        <style>
        #menuWerx ul
        {
        padding-left: 0;
        margin-left: 0;
        background-color: #036;
        color: White;
        float: left;
        width: 100%;
        font-family: arial, helvetica, sans-serif;
}

        #menuWerx ul li { display: inline; }

        #menuWerx ul li a
        {
        padding: 0.2em 1em;
        background-color: #036;
        color: White;
        text-decoration: none;
        float: left;
        border-right: 1px solid #fff;
}

        #menuWerx ul li a:hover
        {
        background-color: #369;
        color: #fff;
}

        #menuWerx ul li#active a
        {
        background-color: #369;
}
        </style>
        "
;
?>
//*********************************************************************************************
menuwerx.class.php:
PHP Code:
<?php
        
/*
        *    menuWerx - Menu creation class for PHP
        *    version:     1.0a
        *    File:        menuwerx.class.php
        *    Liscense:    GNU - http://www.gnu.org/copyleft/gpl.html
        *    Author:        Heath Nail
        *    Website:    http://www.Clear-Mind.com
        *    Email:        heathnail@hotmail.com
        */
        
class menuWerx
        
{
                
//var $aSettings;

                
function menuWerx()
                {
                        
// Initialization functions - please do not edit unless you know
                        // what you are doing.

                        
include('menuwerx.config.php');
                        
$this->getActiveLink();
                        if(
$this->active_link != "")
                        {
                                foreach(
$this->pages as $key => $page)
                                {
                                        if(
$page['link'] == $this->active_link)
                                        {
                                                
$this->pages[$key]['active'] = true;
                                        }
                                }
                        }
                        
$this->pages $this->positionHori($this->pages);
                        
$this->pages $this->makeURLs($this->pages);
                        
$this->pages $this->wrapLI($this->pages);
                        
$this->pages $this->wrapUL($this->pages);
                        
$this->pages $this->wrapDiv($this->pages);
                        
$this->pages $this->addStyle($this->pages);
                        
$this->showMenu();
                }

                
// Debug function - just echos values...

                
function deBug($id ""$value)
                {
                        if(
$this->aSettings['debug'] == true)
                        {
                                if(
is_array($value) || is_object($value))
                                {
                                        echo(
'<pre>');
                                        echo(
"Debug info: $id \n");
                                        
print_r($value);
                                        echo(
'</pre>');
                                }
                                else
                                {
                                        echo(
"Debug info: $id \n");
                                        echo(
$value);
                                        echo(
'<br>');
                                }
                        }
                }

                
// Determines the active page

                
function getActiveLink()
                {
                        
$this->script getenv('SCRIPT_NAME');
                        
$this->script_exp explode('/'$this->script);
                        
$this->script_exp_cnt count($this->script_exp) - 1;
                        
$this->active_link $this->script_exp[$this->script_exp_cnt];
                }

                
// Determines the horizontal or vertical position of the menu li elements.  This
                // position is determined by the order of the array.  1st element is
                // 'far_left' or 'top', in between elements are 'between', and last element is
                // 'far_right' or 'bottom'.  The value is stored in the ['position'] index.

                
function positionHori($pages_array)
                {
                        
$num_of_pages count($pages_array);
                        foreach(
$pages_array as $key => $page)
                        {
                                if(
$key == && $this->aSettings['orientation'] == "horizontal")
                                {
$pages_array[$key]['position'] = 'far_left';}
                                if(
$key == && $this->aSettings['orientation'] == "vertical")
                                {
$pages_array[$key]['position'] = 'top';}
                                if(
$key != && $key != $num_of_pages)
                                {
$pages_array[$key]['position'] = 'between';}
                                if(
$key == $num_of_pages && $this->aSettings['orientation'] == "horizontal")
                                {
$pages_array[$key]['position'] = 'far_right';}
                                if(
$key == $num_of_pages && $this->aSettings['orientation'] == "vertical")
                                {
$pages_array[$key]['position'] = 'bottom';}
                        }
                        
$this->deBug("position"$pages_array);
                        return 
$pages_array;
                }

                
// Takes pages array and adds ['URL'] index with the actual URL as
                // the value

                
function makeURLs($pages_array)
                {
                        if(
$this->aSettings['mode'] == 'transitional')
                        {
                                foreach(
$pages_array as $key => $page)
                                {
                                        
$pages_array[$key]['URL'] = '<a title="' $page['title'] .
                                        
'" target="' $page['target'] .
                                        
'" href="' $page['link'] .'">' .
                                        
$page['name'] . '</a>';
                                }
                                
$this->deBug("makeURLs"$pages_array);
                                return 
$pages_array;
                        }

                        if(
$this->aSettings['mode'] == 'strict')
                        {
                                foreach(
$pages_array as $key => $page)
                                {
                                        
$pages_array[$key]['URL'] = '<a title="' $page['title'] .
                                        
'" href="' $page['link'] .'">' .
                                        
$page['name'] . '</a>';
                                }
                                
$this->deBug("makeURLs"$pages_array);
                                return 
$pages_array;
                        }
                }

                
// Wraps the pages array ['URL'] with li tags and creates another index
                // of ['li']

                
function wrapLI($pages_array)
                {
                        foreach(
$pages_array as $key => $page)
                        {
                                if(
$page['position'] == "between" && !isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li>' $page['URL'] . '</li>' "\n";
                                }
                                if(
$page['position'] == "between" && isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li id="active">' $page['URL'] . '</li>' "\n";
                                }
                                
// Left
                                
if($page['position'] == "far_left" && !isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li class="far_left">' $page['URL'] . '</li>' "\n";
                                }
                                if(
$page['position'] == "far_left" && isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li id="active" class="far_left">' .
                                        
$page['URL'] . '</li>' "\n";
                                }
                                
// Right
                                
if($page['position'] == "far_right" && !isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li class="far_right">' $page['URL'] . '</li>' "\n";
                                }
                                if(
$page['position'] == "far_right" && isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li id="active" class="far_right">' .
                                        
$page['URL'] . '</li>' "\n";
                                }
                                
// Top
                                
if($page['position'] == "top" && !isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li class="top">' $page['URL'] . '</li>' "\n";
                                }
                                if(
$page['position'] == "top" && isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li id="active" class="top">' .
                                        
$page['URL'] . '</li>' "\n";
                                }
                                
// Bottom
                                
if($page['position'] == "bottom" && !isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li class="bottom">' $page['URL'] . '</li>' "\n";
                                }
                                if(
$page['position'] == "bottom" && isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li id="active" class="bottom">' .
                                        
$page['URL'] . '</li>' "\n";
                                }
                        }
                        
$this->deBug("wrapLI"$pages_array);
                        return 
$pages_array;
                }

                
// Wraps the menu in <ul> tags and returns the finished menu as a string.

                
function wrapUL($pages_array)
                {
                        foreach(
$pages_array as $key => $page)
                        {
                                
$li_st.= $page['li'];
                        }
                        
$ul_st.= '<ul id="menuWerx-list">' "\n";
                        
$ul_st.= $li_st;
                        
$ul_st.= '</ul>';
                        
$pages_array['ul'] = $ul_st;

                        
$this->deBug("wrapUL"$pages_array['ul']);
                        return 
$pages_array;
                }

                
// Wraps the menu in <div> tags and returns the finished menu as a string.

                
function wrapDiv($pages_array)
                {
                        
$pages_array['div'] = '<div id="menuWerx">' "\n" $pages_array['ul'] . "\n" '</div>' "\n";
                        
$this->deBug("wrapDiv"$pages_array['div']);
                        return 
$pages_array;
                }

                
// Add the inline styles

                
function addStyle($pages_array)
                {
                        if(
$this->aSettings['useInlineStyle'] != 'yes')
                        {
                                
$pages_array['menu'] = "\n" '<!--Begin menuWerx-->' $pages_array['div'] . '<!--End menuWerx-->' "\n\n";
                                return 
$pages_array;
                        }
                        if(
$this->aSettings['orientation'] == 'vertical')
                        {
                                
$pages_array['menu'] = "\n" '<!--Begin menuWerx-->' "\n" $this->css['vertical'] . $pages_array['div'] . '<!--End menuWerx-->' "\n\n";
                                return 
$pages_array;
                        }
                        if(
$this->aSettings['orientation'] == 'horizontal')
                        {
                                
$pages_array['menu'] = "\n" '<!--Begin menuWerx-->' .  "\n" $this->css['horizontal'] . $pages_array['div'] . '<!--End menuWerx-->' "\n\n";
                                return 
$pages_array;
                        }
                }

                
// Echos the menu for the browser

                
function showMenu()
                {
                        echo(
$this->pages['menu']);
                }

        }

?>
__________________
Clear Mind Hosting
Simply is offline   Reply With Quote
Old 03-26-2006, 09:10 PM   PM User | #2
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
I'll spare you my lecture about the virtues of separating your display from your application logic, but I've got to comment on the long 'if' blocks here: if you're typing basically the same thing again and again, chances are you're making things too hard and your code could benefit from some refactoring. Computers are great at doing repetitive stuff, and you can almost always factor out tedium and save yourself and future maintainers time.

For example, if you look at all these conditions, they're very similar:
PHP Code:

                        
foreach($pages_array as $key => $page)
                        {
                                if(
$page['position'] == "between" && !isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li>' $page['URL'] . '</li>' "\n";
                                }
                                if(
$page['position'] == "between" && isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li id="active">' $page['URL'] . '</li>' "\n";
                                }
                                
// Left
                                
if($page['position'] == "far_left" && !isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li class="far_left">' $page['URL'] . '</li>' "\n";
                                }
                                if(
$page['position'] == "far_left" && isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li id="active" class="far_left">' .
                                        
$page['URL'] . '</li>' "\n";
                                }
                                
// Right
                                
if($page['position'] == "far_right" && !isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li class="far_right">' $page['URL'] . '</li>' "\n";
                                }
                                if(
$page['position'] == "far_right" && isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li id="active" class="far_right">' .
                                        
$page['URL'] . '</li>' "\n";
                                }
                                
// Top
                                
if($page['position'] == "top" && !isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li class="top">' $page['URL'] . '</li>' "\n";
                                }
                                if(
$page['position'] == "top" && isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li id="active" class="top">' .
                                        
$page['URL'] . '</li>' "\n";
                                }
                                
// Bottom
                                
if($page['position'] == "bottom" && !isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li class="bottom">' $page['URL'] . '</li>' "\n";
                                }
                                if(
$page['position'] == "bottom" && isset($page['active']))
                                {
                                        
$pages_array[$key]['li'] = '<li id="active" class="bottom">' .
                                        
$page['URL'] . '</li>' "\n";
                                }
                        } 
The only difference in effect is whether or not the active id is assigned and the name of the class. So, if you cater to these differences you can simplify the whole thing and remove redundant tests by concentrating on only the dynamic part and consolidating the static:

PHP Code:
foreach($pages_array as $key => $page)
{
    
// this is always going to be tested, why not move it out of the conditionals and get it over with?
    
$activeid = (isset($page['active'])) ? ' id="active"' '';
    
// what's different about these assignments? the class, and whether or not the active id is assigned
    // in every case but between, the class is the same as $page['position'], so...
    
if ($page['position'] == 'between')
    {
        
$pages_array[$key]['li'] = '<li' $activeid '>' $page['URL'] . '</li>' "\n";
    }
    else
    {
        
$pages_array[$key]['li'] = '<li' $activeid ' class="' $page['position'] . '">' $page['URL'] . '</li>' "\n";
    }

In a somewhat related issue, it doesn't make sense to test an iterative index against a known static to perform some action. Here:
PHP Code:
                       foreach($pages_array as $key => $page)
                        {
                                if(
$key == && $this->aSettings['orientation'] == "horizontal")
                                {
$pages_array[$key]['position'] = 'far_left';}
                                if(
$key == && $this->aSettings['orientation'] == "vertical")
                                {
$pages_array[$key]['position'] = 'top';}
                                if(
$key != && $key != $num_of_pages)
                                {
$pages_array[$key]['position'] = 'between';}
                                if(
$key == $num_of_pages && $this->aSettings['orientation'] == "horizontal")
                                {
$pages_array[$key]['position'] = 'far_right';}
                                if(
$key == $num_of_pages && $this->aSettings['orientation'] == "vertical")
                                {
$pages_array[$key]['position'] = 'bottom';}
                        } 
If you want to assign something to $array[$key] when $key is 1, cut out the middle man and assign to $array[1]:
PHP Code:
// set the starting point. no need to test iteratively since the value is known
$pages_array[1]['position'] = ($this->aSettings['orientation'] == 'horizontal') ? 'far_left' 'top';
// fill in the middle
for ($key 2$key $num_of_pages; ++$key)
{
    
$pages_array[$key]['position'] = 'between';
}
// set the endpoint
$pages_array[$num_of_pages]['position'] = ($this->aSettings['orientation'] == 'horizontal') ? 'far_right' 'bottom'
Hope this helps, or at least that it isn't completely insufferable.

Last edited by ralph l mayo; 03-26-2006 at 09:12 PM..
ralph l mayo is offline   Reply With Quote
Old 03-28-2006, 05:22 AM   PM User | #3
Simply
New Coder

 
Join Date: Feb 2006
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Simply is an unknown quantity at this point
As far as the seperation of display from code, do you mean use some type of templating system like .tpl files in phpbb? Where could I find examples of different ways to seperate display from code?

Thanks for your input.
__________________
Clear Mind Hosting
Simply is offline   Reply With Quote
Old 03-29-2006, 05:02 PM   PM User | #4
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
Yeah, templating is good. I don't like embedded HTML but it's not so bad since it has traditionally been pretty much PHP's whole raison d'etre, but CSS really needs to stay in CSS files. It's good in your implementation that it's so easy to change in the config file, but it'd be even easier to just include a sample CSS file that users can implement or modify just by changing the <head> section of the file they're using the script on to point to other stylesheets, as is the standard.

As far as the ways to implement templates, your choices are basically flat or hierarchal. The former is easier to implement and basically involves str_replace()ing a lot of stuff. PHPbb used to do it like this, perhaps they still do: it's basically HTML files with things like @@USER_NAME@@ that are replaced at runtime by an engine class that serves as a go-between.

Hierarchal implementations lend themselves better to XHTML, because XHTML is (supposed to be) well-formed XML, and it can be parsed with DOM/SAX/what have you. In the same way flat methods use @@*@@ or the like to distinguish their area of concern, it can be helpful to use an XML namespace to identify your dynamic portions (ie, <yourtag:username /> instead of just <username>, which also serves to prevent naming conflicts).

I've posted a DOM based template engine in this section, but you'll want to examine it carefully for stupidity if you want to try it out :]. It's got some problems and my edit window has expired.
ralph l mayo is offline   Reply With Quote
Old 03-29-2006, 06:43 PM   PM User | #5
Simply
New Coder

 
Join Date: Feb 2006
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Simply is an unknown quantity at this point
How long is the edit window?
__________________
Clear Mind Hosting
Simply is offline   Reply With Quote
Old 03-29-2006, 06:51 PM   PM User | #6
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
Quote:
Originally Posted by vBulletin message
The administrator has specified that you can only edit messages for 10080 minutes after you have posted
a week
ralph l mayo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:00 AM.


Advertisement
Log in to turn off these ads.