php+mysql实现的无限分类方法类定义与使用示例
本文实例讲述了php+mysql实现的无限分类方法类定义与使用。分享给大家供大家参考,具体如下:
创建数据库以及表
CREATEDATABASE`sortclass`DEFAULTCHARSETutf8; CREATETABLEIFNOTEXISTS`class`( `cid`mediumint(8)unsignedNOTNULLauto_increment, `pid`mediumint(8)unsignedNOTNULL, `cname`varchar(50)NOTNULL, PRIMARYKEY(`cid`), KEY`pid`(`pid`) )ENGINE=MyISAMDEFAULTCHARSET=utf8;
header("Content-type:text/html;charset=utf-8"); //连接数据库 $link=mysql_connect('localhost','root','eric')ordie(mysql_error()); mysql_select_db('sortclass',$link); //无限分类类库 classSortClass{ var$data=array(); var$child=array(-1=>array()); var$layer=array(-1=>-1); var$parent=array(); var$link; var$table; functionSortClass($link,$table){ $this->setNode(0,-1,'顶极节点'); $this->link=$link; $this->table=$table; $node=array(); $results=mysql_query('select*from'.$this->table.'',$this->link); while($node=mysql_fetch_assoc($results)){ $this->setNode($node['cid'],$node['pid'],$node['cname']); } } functionsetNode($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; } functiongetList(&$tree,$root=0){ foreach($this->child[$root]as$key=>$id){ $tree[]=$id; if($this->child[$id])$this->getList($tree,$id); } } functiongetValue($id){return$this->data[$id];} functiongetLayer($id,$space=false){ return$space?str_repeat($space,$this->layer[$id]):$this->layer[$id]; } functiongetParent($id){return$this->parent[$id];} functiongetParents($id){ while($this->parent[$id]!=-1){ $id=$parent[$this->layer[$id]]=$this->parent[$id]; } ksort($parent); reset($parent); return$parent; } functiongetChild($id){return$this->child[$id];} functiongetChilds($id=0){ $child=array($id); $this->getList($child,$id); return$child; } functionaddNode($name,$pid){ mysql_query("insertinto$this->table(`pid`,`cname`)values('$pid','$name')",$this->link); } functionmodNode($cid,$newName){ mysql_query("update$this->tableset`cname`='$newName'where`cid`=$cid",$this->link); } functiondelNode($cid){ $allChilds=$this->getChilds($cid); $sql=''; if(empty($allChilds)){ $sql="deletefrom$this->tablewhere`cid`=$cid"; }else{ $sql='deletefrom'.$this->table.'where`cid`in('.implode(',',$allChilds).','.$cid.')'; } mysql_query($sql,$this->link); } functionmoveNode($cid,$topid){ mysql_query("update$this->tableset`pid`=$topidwhere`cid`=$cid",$this->link); } } //函数 functionback(){ echo'window.location.href="test.php?"rel="externalnofollow"+newDate().getTime();'; exit; } //声成select functionmakeSelect($array,$formName){ global$tree; $select=' '; foreach($arrayas$id){ $select.=' '.$tree->getLayer($id,'|-').$tree->getValue($id).""; } return$select.''; } $tree=newSortClass($link,'`class`'); $op=!empty($_POST['op'])?$_POST['op']:$_GET['op']; if(!empty($op)){ if($op=='add'){ $tree->addNode($_POST['cname'],$_POST['pid']); back(); } if($op=='mod'){ $tree->modNode($_POST['cid'],$_POST['cname']); back(); } if($op=='del'){ $tree->delNode($_GET['cid']); back(); } if($op=='move'){ $tree->moveNode($_POST['who'],$_POST['to']); back(); } } $category=$tree->getChilds(); ?> body{font-size:12px;} ul{list-style:none;} a{cursor:pointer;} function$(e){returndocument.getElementById(e);} functionmod(cid){ $('cid').value=cid; $('op').value='mod'; $('name').style.border='1pxsolidred'; } 名称: 添加到:=makeSelect($category,'pid')?>
移动分类
=makeSelect($category,'who')?>移动到:=makeSelect($category,'to')?> '.$tree->getLayer($id,'|-').$tree->getValue($id).'
Del Edit'; } ?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。