PHP Build Menu _php Tutorial

Source: Internet
Author: User
For people who have used C/s, the menu selection function is the most basic operation, in PHP, it is also easy to implement the menu function, the method is as follows:
menu.php:

//
Author: Christine
Email:greenchn@163.net
//
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%s
,
$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%s
,
$indent. $this->indent,
$item [href],
(!empty ($item [target])? target= ". $item [target].":), $item [name]);
echo "n";
}
}
}
Else
{
printf (%s%s
,
$indent. $this->closed,
$PHP _self,
$urlname, $QUERY _string,
$this->name);
echo "n";
}
}
}
?>



menu2.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 ();
?>

You can see the menu effect by executing menu2.php.

http://www.bkjia.com/PHPjc/532235.html www.bkjia.com true http://www.bkjia.com/PHPjc/532235.html techarticle for people who have used C/s, the menu selection function is the most basic operation, in PHP, it is also easy to implement the menu function, the method is as follows: menu.php:////Author: Christine/...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.