我是小佳 ,未注明原创的,均为转载。
PHP和CSS样式来实现菜单
上一篇 /
下一篇 2006-10-16 16:52:22 / 天气: 晴朗
/ 心情: 高兴
/ 个人分类:PHP杂谈
CSS:
ur { margin-left: 0; padding-left: lem; } li{ list-style-type: disc; list-style-image:url(down.png); } li.file{ list-style-type:square; list-style-image:url(right.png); } a:link, a:active, a:visited{ color: #000; font-family: Verdana; font-size:13px; text-decoration:none; background-color:#FFF; display:block; width:125px; border:1px solid #CCC; } a:hover{ background-color:#CCC; } a:link.live, a:active.live, a:visited.live, a:over.live{ border: 1px dotted #808080; } |
javascript:;" onClick="javascript:tagshow(event, 'PHP');" target="_self">PHP脚本:
<html> <head> <title>advanced bulleted-list menu</title> <link rel="stylesheet" title="default" href="style.css" /> <script type="text/javascript"> <!-- function toggle(id) { if(document.getElementById) { var el = document.getElementById(id); el.style.display = (el.style.display == 'none') ? 'block' : 'none'; } } //--> </script> </head> <body>
<?php
// menu title => menu target $menu = array('Menu One'=>array('Page One'=>'blist_menu.php?pg=1', 'Page Two'=>'blist_menu.php?pg=2'), 'Menu Two'=>array('Page Three'=>'blist_menu.php?pg=3', 'Page Four'=>'blist_menu.php?pg=4'), 'Menu Three'=>array('Page Five'=>'blist_menu.php?pg=5', 'Menu Four'=>array('Page Six'=>'blist_menu.php?pg=6', 'Page Seven'=>'blist_menu.php?pg=7')));
// alternative method to using cookies function array_search_recursive($needle, $haystack) { $pos = null; $keys = array_keys($haystack); while(!$pos && (list($garbage, $value)=each($keys))) { if(is_scalar($haystack[$value])) { if($haystack[$value] === $needle) $pos[] = $value; } elseif(is_array($haystack[$value])) { if($pos = array_search_recursive($needle, $haystack[$value])) array_unshift($pos, $value); } } return $pos; }
// recursive function to draw menu function draw_menu($menu, $preserve, &$id) { if($id == 0) echo "<div id=\"$id\">\r\n<ul>\r\n"; else echo "<div id=\"$id\" style=\"display:none;\">\r\n<ul>\r\n"; $id += 1;
foreach($menu as $key=>$value) { if(is_array($value)) { if(@in_array($key, $preserve)) $toggle = $id; echo "<li><a href=\"#\" onclick=\"toggle($id);\">$key</a></li>\r\n"; draw_menu($value, $preserve, $id); } else { echo "<li class=\"file\">"; if(@in_array($key, $preserve)) echo "<a class=\"live\" href=\"$value\">$key</a>"; else echo "<a href=\"$value\">$key</a>"; echo "</li>\r\n"; } } echo "</ul>\r\n</div>\r\n"; if(isset($toggle)) echo "<script language=\"javascript\">toggle($toggle);</script>\r\n"; }
$id = 0; $base = basename($_SERVER['PHP_SELF']); $self = isset($_SERVER['QUERY_STRING']) ? $base.'?'.$_SERVER['QUERY_STRING'] : $base; $preserve = array_search_recursive($self, $menu); draw_menu($menu, $preserve, $id);
?>
</body> </html> |
里面用了递归,看的我头都大了!!
有几个函数需要注意:
boolis_scalar( mixed var )
如果给出的变量参数var是一个标量,is_scalar()返回TRUE,否则返回FALSE。 标量变量是指那些包含了integer、float、string或boolean的变量,而array、object和resource则不是标量。
intarray_unshift( array &array, mixed var [, mixed ...] )
array_unshift()将传入的单元插入到array数组的开头。注意单元是作为整体被插入的,因此传入单元将保持同样的顺序。所有的数值键名将修改为从零开始重新计数,所有的文字键名保持不变。 返回array数组新的单元数目。
导入论坛
收藏
分享给好友
管理
举报
TAG:
PHP杂谈