// 连接数据库教程
$link = mysql教程_connect('localhost','root','密码') or die(mysql_error());
mysql_select_db('sortclass',$link);
mysql_query("设置名称'gbk'");
//无限分类类库
类排序类{var $data = array();
var $child = array(-1=>array());
var $layer = array(-1=>-1);
var $parent = array();
var $link;
var $表;
函数 sortclass($link, $table){
$this->setnode(0, -1, '顶极节点');
$this->link = $link;
$这个->表= $表;
$节点=数组();
$results = mysql_query('select * from '.$this->table.'',$this->link);
while($node = mysql_fetch_assoc($results)){
$this->setnode($node['cid'],$node['pid'],$node['cname']);
}
}
函数 setnode ($id, $parent, $value){
$parent = $parent?$parent:0;
$this->data[$id] = $value;
$this->child[$id] = array();
$this->child[$parent][] = $id;
$this->parent[$id] = $parent;
$this->layer[$id] = !isset($this->layer[$parent])? 0 : $this->layer[$parent] 1;
}
function getlist (&$tree, $root= 0){
foreach ($this->child[$root] as $key=>$id){
$tree[] = $id;
if ($this->child[$id]) $this->getlist($tree, $id);
}
}
function getvalue ($id){return $this->data[$id];}
function getlayer ($id, $space = false){
return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
}
function getparent ($id){return $this->parent[$id];}
函数 getparents ($id){
while ($this->parent[$id] != -1){
$id = $parent[$this->层[$id]] = $this->parent[$id];
}
ksort($父级);
重置($父级);
返回$父级;
}
function getchild ($id){return $this->child[$id];}
函数 getchilds ($id = 0){
$child = 数组($id);
$this->getlist($child, $id);
返回 $child;
}
函数addnode($name,$pid){
mysql_query("插入$this->表(`pid`,`cname`)值('$pid','$name')",$this->link);
}
函数 modnode($cid, $newname){
mysql_query("更新 $this->table set `cname`='$newname' where `cid` = $cid",$this->link);
}
函数 delnode($cid){
$allchilds = $this->getchilds($cid);
$sql ='';
if(空($allchilds)){
$sql = "从 $this->表中删除`cid` = $cid";
}其他{
$sql = '从'.$this->表中删除。'其中 `cid` in ('.implode(',',$allchilds).','.$cid.')';
}
mysql_query($sql,$this->link);
}
函数 movenode($cid, $topid){
mysql_query("更新 $this->table set `pid`=$topid where `cid` = $cid", $this->link);
}
}
//函数
函数返回(){
echo '';
出口;
}
//选择生成
函数 makeselect($array,$formname){
全局$树;
$select = '';
}
$tree = new sortclass($link,'`class`');
$op = !empty($_post['op']) ? $_post['op'] : $_get['op'];
if(!empty($op)){if($op=='add'){
$tree->addnode($_post['cname'],$_post['pid']);
后退();
}if($op=='mod'){
$tree->modnode($_post['cid'],$_post['cname']);
后退();
}if($op=='del'){
$tree->delnode($_get['cid']);
后退();
}if($op=='移动'){
$tree->movenode($_post['who'],$_post['to']);
后退();
}
}
$category = $tree->getchilds();
?>
顶极节点 del 编辑
|- 1级分类删除 编辑
|- 1级分类删除编辑
|- 1级分类删除编辑
|- |- 2 级分类删除编辑
|- |- |- 3 级分类删除
|- |- |-|- 4 级分类删除
|- 1级分类删除编辑