php建立菜单
时间:2007-02-17 来源:PHP爱好者
对于使用过C/S的人来说,通过菜单选择功能是最基本的操作,在php中,也很容易实现菜单的功能,方法如下:
menu.php:
<?php
//
// 作者: christine
// email: [email protected]
//
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 "n";
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 "n";
}
}
}
else
{
printf('%s<a href="%s?%s=&%s">%s</a><br>',
$indent . $this->closed,
$PHP_SELF,
$urlname, $QUERY_STRING,
$this->name);
echo "n";
}
}
}
?>
menu2.php:
<?php
include('menu.php');
$submenu = new menu('Sub Menu');
$submenu->add('Sub Item 1', 'vote.php3', '_new');
$submenu->add('Sub Item 2', 'vote.php3');
$main = new menu('Main');
$main->add('Main Item 1', 'vote.php3?');
$main->add('Main Item 2', 'vote.php3');
$main->add($submenu);
$main->add('Main Item 3', 'vote.php3');
$second = new menu('Secondary Menu');
$second->add('Secondary Item 1', 'vote.php3');
$second->add('Secondary Item 2', 'vote.php3');
$main->show();
//$second->show();
?>
执行menu2.php就可看到菜单效果。
php爱 好者站 http://www.phpfans.net 文章|教程|下载|源码|论坛.
menu.php:
<?php
//
// 作者: christine
// email: [email protected]
//
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 "n";
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 "n";
}
}
}
else
{
printf('%s<a href="%s?%s=&%s">%s</a><br>',
$indent . $this->closed,
$PHP_SELF,
$urlname, $QUERY_STRING,
$this->name);
echo "n";
}
}
}
?>
menu2.php:
<?php
include('menu.php');
$submenu = new menu('Sub Menu');
$submenu->add('Sub Item 1', 'vote.php3', '_new');
$submenu->add('Sub Item 2', 'vote.php3');
$main = new menu('Main');
$main->add('Main Item 1', 'vote.php3?');
$main->add('Main Item 2', 'vote.php3');
$main->add($submenu);
$main->add('Main Item 3', 'vote.php3');
$second = new menu('Secondary Menu');
$second->add('Secondary Item 1', 'vote.php3');
$second->add('Secondary Item 2', 'vote.php3');
$main->show();
//$second->show();
?>
执行menu2.php就可看到菜单效果。
php爱 好者站 http://www.phpfans.net 文章|教程|下载|源码|论坛.
相关阅读 更多 +