PX : code

Pull Down menu by zhong hongye
Download this code


// pd_menu.inc
<?php
if(!defined(_PDMENU_CLASS_)){
define("_PDMENU_CLASS_",1);



    
define("DEFAULT_MENUPANEL_WIDTH",100);
    
define("MENU_SUB",0x01);
    
define("MENU_BARITEM",0x02);
    
define("MENU_CHECKED",0x04);
    
define("MENU_RCHECKED",0x08);
    
define("MENU_DISABLED",0x10);
    
define("MENU_LINE",0x20);
//    define("MENU_NEWWINDDOW",0x40);
    
define("MENU_ROOT",0x80);
    
$agentwords=split('[/ ]',$HTTP_USER_AGENT);
    
$ie4 false;
    
$ns4 false;
    
    if(
$agentwords[0]=='Mozilla' && $agentwords[1]>=4.0){
        if (
strpos($HTTP_USER_AGENT,'MSIE')){
            
$ie4 true;
            
$ie5 = (substr(strstr($HTTP_USER_AGENT,"MSIE"),5,3)>=5);
        }
        else
            
$ns4 true;
    }

if (!
function_exists('menuitem')){        
class 
menuitem{
    var 
$layerName;
    var 
$submenu;
    var 
$url;
    var 
$status;
    var 
$panelWidth;
    var 
$caption;
    var 
$target;
    function 
menuitem($caption,$status=0,$url='#',$sub=false,$target='')
    {
        static 
$uid=0;
        
$this->caption trim($caption);
        
$this->status=$status;
        
$this->status |= MENU_SUB;
        
$this->layerName='PHP_PDMENU_'.(++$uid);
        if(
$sub)
        
$this->submenu=$sub;
        
$this->url=trim($url);
        
$this->target trim($target);
        
$this->panelWidth DEFAULT_MENUPANEL_WIDTH;
    }
};
}
if(!
function_exists('pdmenu')){
class 
pdmenu{
    var 
$layerName;
    var 
$submenu;
    var 
$url;
    var 
$status;
    var 
$id;
    var 
$panelBgColor,$panelWidth,$itemStyle,$itemSltStyle;//style is css classname.
    
var $barBgColor,$barStyle,$barSltStyle,$barItemWidth,$barWidth;
    var 
$subPic,$chkPic,$rchkPic;
    var 
$posx;
    var 
$barTemplate;
    var 
$itemTemplate;
    var 
$menuitems;
//for old browsers
    
var $barPreffix='';
    var 
$barSuffix='';
    var 
$bitemPreffix='<font color=white>';
    var 
$bitemSuffix='</font>';
#######################################################################

    
function pdmenu()
    {
        static 
$menuid=0;
        
$this->id=++$menuid;
        
$this->layerName='PDMENU_'.$this->id;
        
$this->status=MENU_ROOT;
        
$this->posx=0;
        
$this->panelWidth DEFAULT_MENUPANEL_WIDTH;
        
$this->subPic "<img src='pop_arw.gif' align=right vspace=6 border=0 >";
        
$this->barBgColor='lightblue';
    }
    function 
initMenu($menuStr,$splitChar='|'){
        
$this->menuitems[0]=&$this;
        
$n $n1 0;
        
$menuitems=explode("\n",$menuStr);
        
$context[0]=0;$level=0;$size[0]=0;
    while(list(
$key,$menuitem)=each($menuitems)){
            if((!
$menuitem) || $menuitem=='') continue;
            list(
$lv,$caption,$status,$url,$target)=explode($splitChar,$menuitem);
            
$lv strlen($lv)+1;
            if(
$lv>$level){
                if(
$lv!=$level+1) return false;
                
$this->menuitems[$context[$level]]->status|=MENU_SUB;
                
$level++;$size[$level]=0;
            }else if (
$lv $level)
                
$level=$lv;
            
$this->menuitems[++$n]=new menuitem($caption,intval($status),$url,false,$target);
            
$pcx=$context[$level-1];
            
$context[$level] = $this->menuitems[$pcx]->submenu[$size[$level-1]++]=$n;
        }
        return 
ture;
    }
    function 
activateMenu($menu=false)
    {
        global 
$ie4,$ie5,$ns4;
        if(!(
$ie4 || $ns4 || $ie5))
            return;
        for(
$i=0;$i<count($this->menuitems);$i++){
            if (
$this->menuitems[$i]->submenu){
                
$layerName $this->menuitems[$i]->layerName;
                if(
$ns4)
                    echo 
"setTimeout(\"initMenu('$layerName')\",40);\n";
                if(
$ie4)
                    echo 
"initMenu('$layerName');\n";
            }
         }
    }
    function 
showMenu($menu)
    {
        global 
$ie4,$ie5,$ns4;
        if(!(
$ie4 || $ns4 || $ie5)){
            
$this->showBarOnly();
            return ;
        }
        for(
$i=0;$i<count($this->menuitems);$i++){
            if (
$this->menuitems[$i]->submenu){
                
$this->genSubMenuLayer($i);
                
$this->genSubMenuStr($i);
            }
        }
    }

    function 
genSubMenuStr($itemId)
    {
    
$panelItem $this->menuitems[$itemId];
    
$items=$panelItem->submenu;
    
$layerName=$panelItem->layerName;
    
$panelColor $this->panelBgColor;
    
$barBgColor $this->barBgColor;
    echo 
"\n<script language='JavaScript'>\n<!--//Hide from old browsers\n";
    
$i=0;
    if(
$panelItem->status&MENU_ROOT){
        
$style $this->barStyle;
        
$sltStyle $this->barSltStyle;
        
$width $this->barWidth;
        
$template =  
<<<EOD
("<font class=%menuitem=%id%%><a href='%url%' class=%menuitem=%id%% %addons% "
 +"onMouseover=\"bitemSelect('%parentName%','%id%','%childName%',this);\" >" 
+"|%caption%&nbsp;&nbsp;&nbsp;</a></font>")
EOD;
        }
    else {
        
$style $this->itemStyle;
        
$sltStyle $this->itemSltStyle;
        
$width $this->panelWidth;
        
$template 
<<<EOD
("<tr><td class=%menuitem=%id%% nowrap> %SubmenuMark%&nbsp;&nbsp;<A class=%menuitem=%id%% "+
"onMouseover=\"pitemSelect('%parentName%','%id%','%childName%',this);\" "
+ "href='%url%' %addons% >%caption%</A></td></tr>")
EOD;
        }
    echo 
"strHtml='';\n";
    if(!(
$panelItem->status&MENU_ROOT)){
    echo 
"strHtml='<table border=0 width=\'100%\' cellpadding=1 cellspacing=1 bgcolor=black ><tr><td>';\n";
    echo 
"strHtml+='<table border=0 width=\'100%\' cellpadding=0 cellspacing=0 bgcolor=$barBgColor>';\n";
    }
    while(list(
$key,$id)=each($items)){
        
$item $this->menuitems[$id];
        
$caption $item->caption;
        if(
$item->status&MENU_LINE){
            
//echo $item->status;
            
echo "strHtml+='<tr><td class=$style nowrap>$caption</td></tr>';\n";
            continue;
        }
        
$urladdons='';
        if(
$item->target)
            
$urladdons .= ("target=".$item->target);
        
$str=str_replace('%id%',++$i,$template);
        
$str=str_replace('%parentName%',$layerName,$str);
        
$str=str_replace('%caption%',$caption,$str);
        
$str=str_replace('%childName%',$item->submenu?$item->layerName:'',$str);
        
$str=str_replace('%url%',$item->url,$str);
        
$str=str_replace('%addons%',$urladdons,$str);
        if(
$item->submenu
            
$str str_replace('%SubmenuMark%',$this->subPic,$str);
    else
            
$str str_replace('%SubmenuMark%','',$str);
        echo 
"strHtml+=$str;\n";
        }
    if(!(
$panelItem->status&MENU_ROOT)){
        echo 
"strHtml += '</table></td></tr></table>';";
    }
    echo 
"setMenuProp('$layerName','originalHtml',strHtml);\n";
    echo 
"setMenuProp('$layerName','strStyle','$style');\n";
    echo 
"setMenuProp('$layerName','strSltStyle','$sltStyle');\n";
    echo 
"setMenuProp('$layerName','menuDefaultWidth',$width);\n";
    echo 
"//-->\n</Script>";
    }

    function 
genSubMenuLayer($itemId)
    {
    global 
$ie4,$ns4;
    
$panelItem $this->menuitems[$itemId];
    if(
$itemId==0){//for MenuBar ,NoHide for Menubar
        
$posx=$this->posx;
        
$bgColor=$this->barBgColor;
        
$layerName=$panelItem->layerName;
        if(
$ie4){
            echo 
"<div id='$layerName' style='position:absolute;left:$posx ;background-Color:$bgColor;' \n";
            echo 
"onMouseover=\"aMenuOpened[0]='$layerName';stopHiMenu();\" \n";
            echo 
"onMouseout=\"delayHiMenu();\"> \n";
            
$this->showBarOnly();
            echo 
"</div>\n";
            }
        else if(
$ns4) {
            echo 
"<layer id='$layerName' left=$posx bgcolor=$bgColor ";
            echo 
"onMouseover=\"aMenuOpened[0]='$layerName';\" \n";
            echo 
"onMouseout=\"delayHiMenu();\"> \n";
            
$this->showBarOnly();
            echo 
"</layer>";
            }
        return;
    }
    
$bgColor=$this->panelBgColor;
    
$width=$this->panelWidth;
    
$layerName=$panelItem->layerName;
    
$fillin=($panelItem->submenu)?
        
str_repeat('<br>',count($panelItem->submenu)) : '' ;
    if(
$ie4){
        echo 
"<div id='$layerName' style='position:absolute;width:$width;background-Color:$bgColor;visibility:hidden' ";
        echo 
"onMouseout=\"delayHiMenu();\" ";
        echo 
"onMouseOver=\"stopHiMenu();return false;\"> \n";
        echo 
$fillin;
        echo 
"</div>";
    }
    else if(
$ns4) {
        echo 
"<layer id='$layerName' WIDTH=$width bgcolor=$bgColor VISIBILITY=HIDE ";
        echo 
"onMouseout=\"delayHiMenu();\" ";
        echo 
"onMouseOver=\"stopHiMenu();return false\"> ";
        echo 
$fillin;
        echo 
"</layer>";
        }
    }
    function 
showBarOnly(){//function only for old browsers
        
echo $this->barPreffix;
        
$template "<a href='%url%' target='%target%'>{$this->bitemPreffix}|%caption%&nbsp;&nbsp;&nbsp;{$this->bitemSuffix}</a>";
        echo 
"<Table BGCOLOR={$this->barBgColor} border=0 cellspacing=0 cellpadding=0";
        echo 
" hspace=0 vspace=0><tr><td>";
        while(list(
$key,$id)=each($this->submenu)){
            
$item $this->menuitems[$id];
            
$str str_replace('%caption%',$item->caption,$template);
            
$str str_replace('%url%',$item->url,$str);
            
$str str_replace('%target%',$item->target,$str);
            echo 
$str;
        }
        echo 
"</td></tr></table>";
        echo 
$this->barSuffix;
    }

};
//end define pdmenu class
}
}
?>
<?php 
if($ie4||$ns4||$ie5){//for old browsers?>
<Script language='JavaScript'>
<!--// For old browsers
function bitemSelect(strParent,nId,strChild,refChild)
{
    //Get menu position
    //reset layer
    stopHiMenu();
    if (ie4||ie5){
        posx=document.all(strParent).offsetLeft+refChild.offsetLeft;
        posy=document.all(strParent).offsetTop+document.all(strParent).offsetHeight;
    }
    if(ns4){
        posx=document.layers[strParent].pageX+refChild.x;
        posy=document.layers[strParent].pageY+document.layers[strParent].clip.height;
    }
    strOriginalHtml = getMenuProp(strParent,'originalHtml');
    strStyle = getMenuProp(strParent,'strStyle');
    strSltStyle = getMenuProp(strParent,'strSltStyle');
    reg = new RegExp("%menuitem="+nId+"%", "g");
    strOriginalHtml=strOriginalHtml.replace(reg,"'"+strSltStyle+"'");
    strOriginalHtml=strOriginalHtml.replace(/%menuitem=(\w+)%/g,"'"+strStyle+"'");
    for(i=nMenuLevel;i>1 ;i--){
        getObj(aMenuOpened[i]).visibility=hidden;
        aIds[i]=-1;
    }
    nMenuLevel = 1;aIds[1]=-1;
    if(i==1){
        if (strChild==aMenuOpened[1])
            return false;
        getObj(aMenuOpened[1]).visibility=hidden;
    }
        aIds[0]=nId;
    aMenuOpened[1]=strChild;
    showPanel(strParent,strChild,true,posx,posy);
    delayWrite(strParent,strOriginalHtml);
    //writeHtml(strParent,strOriginalHtml);
    return false;
}
function pitemSelect(strParent,nId,strChild,refChild)
{
    //Get menu position
    stopHiMenu();
    if (ie5){
        px=document.all[strParent].offsetLeft;
        pw=document.all[strParent].offsetWidth;
        if(strChild!='')
            cw=document.all[strChild].offsetWidth;
        posy=document.all[strParent].offsetTop+refChild.offsetParent.offsetTop;
        innerWidth = document.body.clientWidth;
    }else if(ie4){
        px=document.all[strParent].offsetLeft;
        pw=document.all[strParent].offsetWidth;
        if(strChild!='')
            cw=document.all[strChild].offsetWidth;
        posy=document.all[strParent].offsetTop
                    +refChild.offsetParent.offsetParent.offsetTop;
        innerWidth = document.body.clientWidth;
    }
    if (ns4){
        px=document.layers[strParent].pageX;
        pw=getMenuProp(strParent,'menuDefaultWidth');
        if(strChild!='')
            cw=getMenuProp(strChild,'menuDefaultWidth');
        innerWidth = window.innerWidth;
        posy=document.layers[strParent].pageY+refChild.y;
    }
    if(strChild!='' && px+pw+cw>innerWidth)
        posx = px-cw;
    else
        posx = px+pw;
    strOriginalHtml = getMenuProp(strParent,'originalHtml');
    strStyle = getMenuProp(strParent,'strStyle');
    strSltStyle = getMenuProp(strParent,'strSltStyle');
    reg = new RegExp("%menuitem="+nId+"%", "g") ;
    strOriginalHtml=strOriginalHtml.replace(reg,"'"+strSltStyle+"'");
    strOriginalHtml=strOriginalHtml.replace(/%menuitem=(\w+)%/g,"'"+strStyle+"'");
    for(i=nMenuLevel;i>0 && aMenuOpened[i]!=strParent && aMenuOpened[i]!=strChild;i--){
        getObj(aMenuOpened[i]).visibility=hidden;
        aIds[i]=-1;
    }
    nMenuLevel=i;
    if (strChild == aMenuOpened[i]){
        aIds[i-1]=nId;
        return false;
    }
    if(nId==aIds[i]) return false;
    aIds[i]=nId;
    if(strChild!=''){
        aMenuOpened[++nMenuLevel]=strChild;
        showPanel(strParent,strChild,true,posx-2,posy);
        }
    delayWrite(strParent,strOriginalHtml);
    return false;
}
function getObj(sObj)
{
  if(ie4) return document.all(sObj).style;
  if(ns4) return document.layers[sObj];
  return false;
}
function showPanel(parent, menu, vis, posx, posy)
{
    initMenu(menu);
    obj = getObj(menu);
    obj.left=posx;obj.top=posy;
        if (obj.visibility==visible && vis)
        return;
    if(vis)
        obj.visibility = visible;
    else 
        obj.visibility = hidden;
}
function delayHiMenu()
{
    menuCloseId=setTimeout("HiAllMenu()",200);
}
function HiAllMenu()
{
    for(i=1;i<=nMenuLevel;i++){
        initMenu(aMenuOpened[i]);
        getObj(aMenuOpened[i]).visibility=hidden;
    }
    initMenu(aMenuOpened[0]);
    nMenuLevel = 0;
    for(i=1;i<10;i++){
        aMenuOpened[i]='';
        aIds[i]=-1;
    }
}
function stopHiMenu()
{
  if (menuCloseId){
    clearTimeout(menuCloseId);
    menuCloseId=false;
    }
}

function writeHtml(cObj,str)
{
    if(ie4)
        document.all[cObj].innerHTML=str;
    else{
        with(document.layers[cObj].document){
            open();
            write(str);
            close();
        }    
    }
}
function delayWrite(strParent,strNewHtml)
{
    if(wrTimeId){
        clearTimeout(wrTimeId);
        wrTimeId=0;
        }
    if(strNewHtml){
        strWrHtml=strNewHtml;
        wrTimeId=setTimeout("delayWrite('"+strParent+"',false)",5);
        }
    else{
        //window.offscreenBuffering = true;
        writeHtml(strParent,strWrHtml);
        //if(ns4){
            //obj = getObj(strParent);
            //obj.clip.width = getMenuProp(strParent,'menuDefaultWidth');
            //}
        wrTimeId=0;
        //window.offscreenBuffering = false;
        }
}

function getMenuProp(menuName,propName)
{
    if (ie4) obj = window.document.all[menuName];
    if (ns4) obj = window.document.layers[menuName];
    return eval("obj."+propName);
}
function setMenuProp(menuName, propName, value)
{
    if (ie4) obj = window.document.all[menuName];
    if (ns4) obj = window.document.layers[menuName];
    eval("obj."+propName+"=value");
}
function initMenu(menu)
{
    strHtml = getMenuProp(menu,'originalHtml');
    strStyle = getMenuProp(menu,'strStyle');
    strDispHtml=strHtml.replace(/%menuitem=(\w+)%/g,"'"+strStyle+"'");
    writeHtml(menu,strDispHtml);
    if (ns4) {
        obj = getObj(menu);
        obj.clip.width = getMenuProp(menu,'menuDefaultWidth');
    }
}

//Global variables
var ie4,ie5;
var ns4;
var strWrHtml;
var wrTimeId=0;
var lastItem = 'aaron';
var menuCloseId;
var aMenuOpened = Array(10),aIds=Array(10);
var nMenuLevel = 0;
//init arrays
for(i=0;i<10;i++){
    aMenuOpened[i]='';
    aIds[i]=-1;
}
if (document.layers) {
    visible = 'show';
    hidden = 'hide';
    ns4 = true;
} else if (document.all) {
    visible = 'visible';
    hidden = 'hidden';
    ie4 = true;
}
if(ie4){
    ver = navigator.appVersion;
    ie5 = (ver.substr(ver.indexOf('MSIE')+5,3)>=5);
    }    
//-->
</Script>
<?php //end of old browsers ?>

// menut.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML>
<HEAD>
    <META content="text/html;" http-equiv="Content-Type">
    <META name=GENERATOR content="MSHTML 5.00.2014.210">
    <TITLE></TITLE>
    <META content="text/html; charset=gb2312" http-equiv="Content-Type">
    <STYLE>
    BODY {
margin-top: 0px;
margin-left: 0px;
margin-bottom: 0px;
margin-right: 0px
}
A {text-decoration: none;}
.item {color:black;background-color:gray;text-decoration: none;}
.sltItem {color:black;background-color:gray;text-decoration: none;font-weight:bold;margin:0;}
.menubarItem {position: absolute; width: 100px; height: 22px; top:0 ;border: 1px solid black; layer-background-color: black;}
.barItem {background-color:lightblue;layer-background-color: lightblue; color:white;text-decoration: none;}
.sBarItem {background-color:black;layer-background-color: lightblue;color:white;text-decoration: none;}
.menuPanel {position: absolute; width: 100px; border: 1px solid black; background-color: black; layer-background-color: black; visibility: hidden; }
.itemAnchor {font-family: Verdana; font-size: 12px; color: white; text-decoration: none; }
.monkey {position:absolute; left:300px; top:150px; visibility: hidden}
</STYLE>
<?php
include("pd_menu.inc");
$menuFirst = new pdmenu;
$menuFirst->panelBgColor='white';
$menuFirst->panelWidth=150;
$menuFirst->itemStyle='item';
$menuFirst->itemSltStyle='sltItem';
$menuFirst->barBgColor='lightblue';
$menuFirst->barStyle='barItem';
$menuFirst->barSltStyle='sbarItem';
$menuFirst->barItemWidth=100;
$menuFirst->barWidth 220;
$menuFirst->posx=200;
$barstatus=MENU_BARITEM;
#You can load it from file
$menuStr=<<<EOD
|Greeting|0|#
.|Hello|0|#
.|Morning|0|#
.|afteronoon|0|#
.|<hr>|32|#
.|More menu|0|#
..|Menu one|0|#
...|End one|0|#
...|End Two|0|#
..|Menu two|0|#
.|<hr>|32|#
.|Hi...|0|#
|Help|0|#
.|Topic...|0|/|_blank
.|Index...|0|/phpdocs/|_blank
.|Search...|0|#
.|<hr>|32|#
.|About...|0|javascript:alert(\"Php pulldown menu Test.Version 1.0\");
EOD;

    if (!
$menuFirst->initMenu($menuStr))
        echo 
"<p>Wrong Format in menu define!!!<br>\n";

/*
    $menuFirst->submenu=array(
        new menuitem('Greeting',barstatus,'#',
        array(
            new menuitem('Hello',0,'#'),
            new menuitem('Morning',0,'#'),
            new menuitem('afternoon',0,'#'),
            new menuitem('<hr>',MENU_LINE),
            new menuitem('More Menu',0,'#',
            array(
                new menuitem('Menu1',0,'#'),
                new menuitem('Menu2',0,'#')
                )),
            new menuitem('<hr>',MENU_LINE),
            new menuitem('Hi...',0,'#')
            )
        ),
        new menuitem('Help',barstatus,'#',
        array(
            new menuitem('Topic...',0,'#'),
            new menuitem('Index...',0,'#'),
            new menuitem('Search...',0,'#'),
            new menuitem('<hr>',MENU_LINE),
            new menuitem('About',0,'javascript:alert(\"Php pulldown menu Test.\nVersion 1.0\");')
            )
        )
    );*/
?>
<Script language = 'JavaScript'>
<!--//
function loadBar()
{
    <?php 
    $menuFirst
->activateMenu();
    
?>
}
//-->
</Script>
<META name=GENERATOR content="MSHTML 5.00.2014.210">
</HEAD>
<BODY bgColor="#ffffff" MARGINHEIGHT=0 text="#000000" onload="loadBar();" MARGINWIDTH=0>
<A  href="javascript:alert(window.screen.availheight);">hello </A>
<BR>
<A  href="javascript:alert(document.body.clientHeight);">hello </A>
<BR>
<A  href="javascript:alert(window.innerHeight);">hello </A>
<BR>
<A  href="#">hello </A>
<BR>
<A  href="#">hello </A>
<BR>
<A  href="#">hello </A>
<BR>
<A  href="#">hello </A>
<BR>
<A  href="#">hello </A>
<BR> 
<?php $menuFirst->showMenu();unset($menuFirst); ?>
</BODY>
</HTML>


Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.