[CODE:]表menu_cls该表结构如下图:
CREATE TABLE IF NOT EXISTS `menu_cls` (
`id` int(4) NOT NULL auto_increment,
`parent_id` int(4) NOT NULL default '0',
`level` int(4) NOT NULL default '0',
`path` varchar(200) collate utf8_unicode_ci default NULL,
`title` varchar(40) collate utf8_unicode_ci default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=29 ;
[CODE:]类Menu_Class.php
class Menu_Class{
private $menu;
public function Set_Array($array){
$this -> menu = $array;
}
public function Path_Line($i){ //随便放了几个符号
switch($i){
case 1:return '▲';break;
case 2:return '★';break;
case 3:return '◆';break;
case 4:return '●';break;
default:return '→';break;
}
}
public function Path_Count($int){ //往前放点空格,分开层次
switch($int){
case 0:return str_repeat(' ',0);break;
default:return str_repeat(' ',$int).$this -> Path_Line($int);break;
}
}
public function Menu_Output(){ //显示菜单
$rows = count($this -> menu);
$level_max = $this -> menu[$rows-1]['level'];
for($i=0;$i<$rows;$i++){
$select .= $this -> Path_Count($this -> menu[$i]['level']-1).$this -> menu[$i]['title'].'';
}
return $select;
}
public function Menu_Select(){ //下拉菜单
$select = ';
$select .= '';
$rows = count($this -> menu);
for($i=0;$i<$rows;$i++){
$select .= '';
}
$select .= '';
return $select;
}
public function Path_Cut($path,$all){ //将PATH分解并获取到当前路径,级别,以及父ID
$all_length = count($all);
if($path==0){ //如果是根目录的话
if(is_array($all)){ //如果已经有N条记录
$cls['path'] = $all[$all_length-1]['id']+1; //现在的路径则为表中最后一个记录的ID+1
}else{
$cls['path'] = $path+1; //否则从1开始
}
$cls['level'] = 1; //根目录 默认层级为一级
$cls['parent_id'] = 0; //父ID为0
}else{
$cls['path'] = $path.','.($all[$all_length-1]['id']+1); //获取路径为父级路径+此条记录ID
$level_arr = split(',',$path);
$cls['level'] = count($level_arr)+1; //获取当前分类级别
$cls['parent_id'] = $level_arr[$cls['level']-2]; //获取父ID
}
return $cls;
}
}
?>
[CODE:]使用 结果如下:
?
require_once(QuiZ_Path.'../Classes/FileName_Set.php');
$link = new ExecSQL(); #SQL操作
$value = new Value_Leach(); #值过滤
$menu = new Menu_Class(); #菜单生成
if($_POST['Submit']=='提交'){
$link -> SetSQL("Select * From ".Base::$Menu_Left." order by id");
$all = $link -> Query_array();
$all_length = count($all);
$cls = $menu -> Path_Cut($value -> _Post('parent_id'),$all); #值分解成$cls['parent_id'],$cls['level'],$cls['path']
$link -> SetSQL("InSert Into ".Base::$Menu_Left."(parent_id,level,title,path)values(:parent_id,:level,:title,:path)");
$link -> bindParam(":parent_id" ,$cls['parent_id'] );
$link -> bindParam(":level" ,$cls['level'] );
$link -> bindParam(":title" ,$value -> _Post('title') );
$link -> bindParam(":path" ,$cls['path'] );
$link -> Query_noreturn();
$link -> Inf_right('','add.php');
}
$link -> SetSQL("Select * From ".Base::$Menu_Left." order by path");
$menu -> Set_Array($link -> Query_array());
$this -> assign('menu',$menu -> Menu_Output());#使用菜单
Smarty -> assign('menu_select',$menu -> Menu_Select());#使用下拉菜单
$Smarty -> display('add.tpl');
?>
SELECT MENU
刚开始考虑的时候,确实迷糊了.不过想清楚了就变得简单了,嘿嘿
几次搬家,图搬丢了 两次使用的博客倒闭... 有空再补上