IT.COM

Collapsable menu in php

Spaceship Spaceship
Watch

axilant

Account Closed
Impact
28
its a class:

I did not program this my self, i found it somewhere, and i fixed a bug...

PHP:
<?
class menu {
    var $name;
    var $items;
    var $open;
    var $closed;
    var $indent;

    function menu($name,
                  $open = '(-)',
                  $closed = '(+)',
                  $indent = '      '
                 )
    {
        $this->items  = array();
        $this->name   = $name;
        $this->open   = $open;
        $this->closed = $closed;
        $this->indent = $indent;
    }

    function add($name, $href = "", $target = "") {
        $n = count($this->items);

        if (is_object($name)) {
            $this->items[$n] = $name;
        } else {
            $this->items[$n]['name'] = $name;
            $this->items[$n]['href'] = $href;
            $this->items[$n]['target'] = $target;
        }
    }

    function show($nest = 0) {
        $urlname = strtr($this->name, ' ', '_');
        $indent = '';
        global $$urlname;
        global $PHP_SELF;
        global $QUERY_STRING;

        if ($nest) {
            $indent = str_repeat($this->indent, $nest);
        }

        if (isset($$urlname)) {
            printf('%s<a href="%s?%s">%s</a><br>',
                   $indent . $this->open,
                   $PHP_SELF,
                   ereg_replace("{$urlname}=&", '', $QUERY_STRING),
                   $this->name);
            echo "";

            while (list(,$item) = each($this->items)) {
                if (is_object($item)) {
                    $item->show($nest + 1);
                } else {
                    printf('%s<a href="%s"%s>%s</a><br>',
                           $indent . $this->indent,
                           $item['href'],
                           (!empty($item['target']) ? ' target="' .
                                                      $item['target'] . '"'
                                                    : ''),
                           $item['name']);
                    echo "";
                }
            }
        } else {
            printf('%s<a href="%s?%s=&%s">%s</a><br>',
                   $indent . $this->closed,
                   $PHP_SELF,
                   $urlname, $QUERY_STRING,
                   $this->name);
            echo "";
        }
    }
}

$submenu = new menu('Sub Menu');
$submenu->add('Sub Item 1', 'link.html', '_new');
$submenu->add('Sub Item 2', 'link.html');

$main = new menu('Main');
$main->add('Main Item 1', 'link.html');
$main->add('Main Item 2', 'link.html');
$main->add($submenu);
$main->add('Main Item 3', 'link.html');

$second = new menu('Secondary Menu');
$second->add('Secondary Item 1', 'link.html');
$second->add('Secondary Item 2', 'link.html');

$main->show();
$second->show();
?>

if you need any help pm me or email me, http://romazom.com/support.php :) well i have to get back to programming
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back